Saturday, 15 September 2012

c# - Interchange Item position of anonymous type -



c# - Interchange Item position of anonymous type -

i have populated sorted list of anonymous type using next code.

var list = service.gettenantsoverview() .tenantsoverview .cast<tenantoverview>() .select(t => new { t.tenantid, t.tenantnumber }) .orderby(t => t.tenantnumber) .tolist();

the problem is, need move 3rd item 1st position. means, want move tenantnumber "any" 1st position. didn't find appropriate method that.

for solution, have made below code.

var item = list.first(f => f.tenantnumber == "any"); list.remove(item); list.insert(0, item);

which works fine.

i want know how can above work using linq in single statement? or possible in single statement?

try initial ordering whether tenant number "any" (note false comes before true default, you'll need create descending):

busconfigurationservice.gettenantsoverview() .tenantsoverview .cast<tenantoverview>() .select(t => new { t.tenantid, t.tenantnumber }) .orderbydescending(t => t.tenantnumber == "any") .thenby(t => t.tenantnumber) .tolist()

c# linq linq-to-entities anonymous-types

No comments:

Post a Comment