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 Articles
- How to order list in custom Item class with linq query
- Entity Framework query to custom object Class with List
- c# find item in list returned by LINQ query and compare its value with another item in list
- Linq query list contains a list with same order
- LINQ custom class query with parameters select
- Returning a custom list class type from LINQ Query - return same type going in that is coming out
- Linq query to custom order by based on number of tags repeated in comma separated list
- Nesting a List<string> in a custom class List via a LINQ SQL Query
- Entity Framework - Linq query with order by and group by
- Linq Query with SUM and ORDER BY
- LINQ with subselect and groupby to get only the latest version of each item in a list
- Using LINQ to Get Sum/ Average of a List with custom objects
- Getting an invalid cast exception when trying to order a list of objects with linq
- Get first item in list from linq query
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- How to write a LINQ to Entities query with List in a "WHERE" condition
- Change the order of List items conditionally with LINQ
- C# Linq Order a list with a reference list
- Linq Query to Compare with List of string values
- LINQ query on object with unknown class
- LINQ query for finding one item in list AND verifying list does not contain another item
- C# : LINQ query to list all empty properties of a class
- query list with linq lambda expressions
- Select multiple rows in order based on list of Ids with LINQ
- C# - Linq cast generic list to custom class
- LINQ query returns old results when source list is re-initialized
- Sitecore7 Linq to Sitecore only works with SearchResultItem but not with Custom Mapped Class
- Calculate difference from previous item in a group with (single) LINQ query
- Sorting a List of Class with LINQ
- C# - Linq optimize code with List and Where clause
- Return true from newly requested record created successfully by entity framework
- How to do a SQL "Where Exists" in LINQ to Entities?
- Lambda Expression to search with conditions and return only 1 latest result
- Count occurence of a string in a list and display it in console
- Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool>
- How do I navigate through multiple relationships using LINQ to Entities?
- How to filter specific columns from a child table Entity Framework
- How do I modify a collection after being calculated?
- I have an Enumerable Where method, how to change it into non-linq
- linq orderby with some condition
- LinQ Error linq joint type inference failed to call 'join' error
- How to set list object c#
- Linq NOT IN query - based on SQL query
- Entity Framework with Condition on SaveChanges
- Filtering Array to return subset of elements in .Net 2.0
- Selecting a new type from linq query
- LINQ to SQL: making a "double IN" query crashes
- linq to entities can not convert int to string
- What is the right way to enumerate using LINQ?
- Improving LINQ query for many-to-many relation