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 items containing the same number of itemlines, 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.


Related Query

More Query from same tag