c# - Linq not populating the other list -
for reason 2nd list list2
isn't being populated, when should.
var list1 = new list<object1>(); //populated list1 here var list2 = new list<object1>(); list1.orderbydescending(o => o.cnt).take(10).select(s => { list2.add(s); return s; });
list2
should have 10 entries now, doesn't? how possible?
it's not working because linq expressions lazily evaluated. select
function not executed until iterate on it, never do.
if add .tolist();
end, should see working.
however, not idiomatic way you're trying do. commenter suggested, better:
var list2 = list1.orderbydescending(o => o.cnt).take(10).tolist();
or add values populated list,
list2.addrange(list1.orderbydescending(o => o.cnt).take(10));