score:0
Accepted answer
this is probably not the nicest way to do this, but since you have the criteria of needing to have all the item
s containing the same number of itemline
s, this is what i could figure out (and would love to see how someone else would do this):
// group the data out of your model organized by the someproperty value
// order it by the number of items in the list so we know the first group will have the maximum number of values we'd need
var orderedgroups = model.items
.selectmany(itm => itm.itemlines)
.groupby(itm => itm.someproperty)
.orderbydescending(grp => grp.count());
// find out how many itemlines you'd want to have in any 1 item
var count = orderedgroups.first().count();
// create the output
var output = new order() {
items = orderedgroups
.select(grp =>
new item()
{
itemlines = new itemline[count]
.select((s,i) => grp.elementatordefault(i) ?? new itemline())
}
)};
update:
given the comment made, i'm assuming your model
is simply an ienumerable of item
rather than an order
. that being the case, simply replace the first line to:
// group the data out of your model ordganized by the someproperty value
// order it by the number of items in the list so we know the first group will have the maximum number of values we'd need
var orderedgroups = model
.selectmany(itm => itm.itemlines)
.groupby(itm => itm.someproperty)
.orderbydescending(grp => grp.count());
all the rest can stay the same.
hope this does the trick.
Source: stackoverflow.com
Related Query
- Collection list to grouped collection list using linq
- Using Linq to create a collection of a custom class from a DbSet that contains a list of grouped entities
- Using Linq to group a list of objects into a new grouped list of list of objects
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- Finding combinations of a grouped list using LINQ in c#
- Using LINQ how do I create a List of one particular field of an entity from a collection entities
- Optimized way to select items from a Collection excluding a list of certain member using Linq
- Get a comma separated list of entity collection using linq
- Get the set of grouped values as a list using linq
- Return List grouped by two properties using linq ASP.NET MVC
- Using Linq to Get a List of Objects to a Distinct Grouped List
- Find the max value in a grouped list using Linq
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- Using LINQ to select a list of every item in a collection, except if it exists in another collection
- Using Linq to group a list of objects into a new grouped list of list of objects with 4 objects in each group
- List of Orders to Grouped Orders using linq
- Check in list and Filter collection using Linq
- c# Linq or code to extract groups from a single list of source data
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Change items bool property in a collection if they match/not match the index containing in other list using LINQ
- Update all objects in a collection using LINQ
- Remove duplicates in the list using linq
- Convert string[] to int[] in one line of code using LINQ
- Searching if value exists in a list of objects using Linq
- Convert list to dictionary using linq and not worrying about duplicates
- How to select values within a provided index range from a List using LINQ
- Using LINQ to Update A Property in a List of Entities
- Return list using select new in LINQ
- Return list of specific property of object using linq
More Query from same tag
- How to get ToDictionary to work in F#?
- How to validate a dropdownlist element with client side validation using MVC?
- LINQ query stores values in KeyValuePair<string, string> list but then cannot store them all with addrange
- Apply IQueryable to another IQueryable / DbContext
- Debugging LINQ Queries
- C# LINQ performance when extension method called inside where clause
- Checking if a string is NOT part of an Array (C#, Linq)
- Which is faster in execution or is more optimized? C#
- How to give property name in string in lambda expression?
- Calling stored proc with LINQ .ExecuteQuery to return non-mapped fields
- How to get minimum unused number from a column in Oracle and Linq?
- Getting wrong data from SQL view when putting it into a C# list
- Create new list of objects for each of the item in another object list using Linq
- Need help turning this LINQ expression into LINQ statement
- Trouble with LINQ Returning Results Set from Object[] Array Within Object
- Retrieving the username from MembershipUser for a join query
- List<ComplexType> : need to do a linq query on a property of the ComplexType
- Ouput Expression Strings with Fully Qualified Enum Values in C#
- Creating objects from xml error {"Value cannot be null.\r\nParameter name: element"}
- LINQ - Dynamic expression to OrderBy
- How to get a list of elements out of a list based on conditions
- Compare two xml and print the difference using LINQ
- Get Current Identity through Linq
- Multiple values for single key
- Compare two lists via LINQ in c#?
- CSV to object model mapping
- LINQ query is not returning the required details
- linq-to-sql count of aggregates based on list of IDs
- Trying to read xml file with LINQ
- LINQ to get distinct column value with count of a string