c# - Order list by value with maximum sequence appearance -



c# - Order list by value with maximum sequence appearance -

i have next list (simplified):

name | brand shirt0 | adidas shirt1 | nike shirt2 | adidas shirt3 | adidas shirt4 | erima shirt5 | nike

i want order brand, , display (a maximum of) 2 of brand in sequence. therefore not display 3 adidas products first, two!

result:

shirt0 | adidas shirt2 | adidas shirt1 | nike shirt5 | nike shirt4 | erima shirt3 | adidas

so takes 2 products every brand, , starts on when no more available.

any ideas how accomplish linq?

i assume product class has next property:

public string brand { get; }

i utilize next approach:

group products brand property wrap result in each grouping in anonymous class additional order property , assign 0 if index within grouping less 2 , 1 if else (will apply first , sec item) apply order based on order property of our anonymous class select product, rid of our anonymous wrapper class

the query below:

var result = list .groupby(x => x.brand) .selectmany(g => g.select((x, i) => new { order = < 2 ? 0 : 1, product = x })) .orderby(x => x.order) .select(x => x.product) .tolist();

i suppose there may improve approach, 1 straight head.

c# .net linq

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -