score:1
If what you are interested on is merely the variants, you could try:
var selections = source.Select(i => i.Variants.OrderByDescending(v => v.isOrdered));
selections
will then be an IEnumerable<IOrderedEnumerable<Variant>>
with three enumerations of variants (based on your sample data), ordered in this manner:
True,True,False
True,False,False
True,True,False,False
UPDATE:
OP has updated the question to require the item as well, so...
There are a few ways to go about this. The least OOP-based, least intrusive one would be to grab the item as well as the sorted variant list into an anonymous type:
var selections = source.Select(i => new
{
Item = i,
SortedVariants = i.Variants.OrderByDescending(v => v.isOrdered)
});
In this case, selections
will be an IEnumerable<'a>
where 'a
is the anonymous type. The type will have two properties: the item that the variants belong to as well as a property called SortedVariants
.
Then there is the simplest, non-reusable way. Every time you access the variants, sort them:
foreach (var item in source)
{
var variants = item.Variants.OrderByDescending(v => v.isOrdered);
//Do something with the variants
}
A more reusable way is to add another property (or method) to the Variant
class that returns the variant list in the desired order:
public class Item
{
public List<Variant> Variants;
public IOrderedEnumerable<Variant> OrderedVariants
{
get { return Variants.OrderByDescending(v => v.isOrdered); }
}
//OR
public IOrderedEnumerable<Variant> GetOrderedVariants()
{
return Variants.OrderByDescending(v => v.isOrdered);
}
}
You can then use that property or method instead.
Lastly, if you have the flexibility to change the current design, you can always hide the list behind an interface and implement a method to add:
public class Item
{
private List<Variant> _variants = new List<Variant>();
public IEnumerable<Variant> Variants
{
get { return _variants.OrderByDescending(v => v.isOrdered); }
}
public void AddVariant(Variant variant)
{
_variants.Add(variant);
}
}
This would my personal favorite since it provides the variants, satisfies the requirement and hides the details of the implementation.
score:0
I would suggest something like the below.
var orderedItems = Items.OrderBy(item => item.Variants.Where(v => v.isOrdered).Count());
This orders the items by how many of it's variants are ordered. You could replace whatever you like in the where.
score:1
if you only want to sort Variant do this :
var SortedVariantItems= Items.Select(x =>new Item {Variants= x.Variants.OrderByDescending(c => c.isOrdered).ToList()})
and if you want In addition to Variant, Items also be sorted(by Variant) do this:
var SortedItems= Items.Select(x =>new Item {Variants= x.Variants.OrderByDescending(c => c.isOrdered).ToList()}).OrderByDescending(x=>x.Variants.Count(c=>c.isOrdered));
Source: stackoverflow.com
Related Query
- How to order list in custom Item class with linq query
- How to write a LINQ to Entities query with List in a "WHERE" condition
- Entity Framework query to custom object Class with List
- How to get the value of class properties with Linq query and IEnumerable
- How do I simplify a LINQ query with multiple .Select() in order to return an IQueryable(int)
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- How do I get this Linq query with custom join into a DataSet?
- C# How to Create a Custom (dynamic) class with Dynamic Linq using XElement in runtime
- How to compile a LINQ query with a List parameter?
- c# find item in list returned by LINQ query and compare its value with another item in list
- How do I write a LINQ to SQL statement with a custom function that needs the compare the current item context to a passed in value?
- Linq query list contains a list with same order
- LINQ custom class query with parameters select
- C# linq query- How to query on inner table with list of values
- how to write a Linq query with a EF code first Many to Many relationship
- How to get the list of class from the output of linq query
- How to get next active item in a list with LINQ
- Using LINQ How to access values of List item inside a Class
- How to get the even item positions in a list with LINQ
- How would a write a LINQ query to compare with a list of values?
- How to to perform Linq query of a list with another list
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- How to built a dictionnary from linq query with Id as Key and Data Class as Value in c#?
- How to get both key and value from list separately with linq query
- Linq how to query a list of items for a combined list of a child collection based on a property of the parent item
- Returning a custom list class type from LINQ Query - return same type going in that is coming out
- How to set order of IQueryable query based on another list using linq in c#?
- How to remove duplicates in List of Class with Linq
- How to query with Linq a List that contains a list of lists that contains a list of lists that contains a list of objects?
- Linq query to custom order by based on number of tags repeated in comma separated list
More Query from same tag
- LINQ-to-SQL: Searching against a CSV
- The WHERE IN clause with two parameters in linq
- LINQ C#: Exclude columns where total = 0, without knowing column names or type?
- C# Linq query search for a single keyword in multiple text files
- Create a sequence of anonymous types from the rows in a DataTable
- Improve Linq query performance that use ToList()
- Linq to Entity Select, Exclude 1st result row?
- Storing the list items into a variable after filtering by using linq
- Need linq help: Trying to add detail to master
- using union on linq compiled queries
- Accessing Elements from multi-level (nested) XML documents in c#
- Grouping multiple records in one single record
- Linq recursive parent child
- How to copy over common base properties using linq
- How do I query an Azure storage table with Linq?
- Creating MemberAcces with ExpressionTree to a complex type
- Each Property-Value in a MyObject-list must be unique
- How do I get a spatial value from EF Core's FromSqlRaw
- Update list of records using LINQ
- Linq's Column In (Select ...) Statement
- How to sum result in each Table and Round from RoundResult in HomePayerResult
- trying to pass generic id's to class constructor
- WebAPI .getJSON function and querying a list with linq returning "Undefined"
- SELECT UNTIL in SQL with Entity Framework
- How to reflect types that have an interface of generic, and get that type of generic
- Localisation/I18n of database data in LINQ to SQL
- Remove 'duplicates' from a list of pairings
- Chopping string with LINQ
- Select record in update mode in linq
- Linq join select all columns and CopyToDataTable