linq - Sorting by age a list class in c# -
i'm beginner , want create implementation prints user information console, ordered age. i've tried use linq without success. code base:
class user { public string name; public string surname; public int age; } interface usersconsumer { public void consume(list<users> collection); }
this tried interface:
list<user> sorteduserlist = collection.orderbydescending(collection => collection.age).tolist();
you need use different variable name inside lambda expression like:
list<user> sorteduserlist = collection.orderbydescending(row => row.age).tolist();
otherwise end error like:
a local variable named 'collection' cannot declared in scope because give different meaning 'collection', used in 'parent or current' scope denote else
edit: noticed 1 thing:
this tried interface:
you can't have implementation in interface. interface more of contract, have implement interface in class , put implementation like:
public class someclass : usersconsumer { public void consume(list<user> collection) { list<user> sorteduserlist = collection.orderbydescending(row => row.age).tolist(); } }
make sure, follow .net naming conventions.
Comments
Post a Comment