How can I get LINQ to return the object which has the max value for a given property? [duplicate]

This will loop through only once. Item biggest = items.Aggregate((i1,i2) => i1.ID > i2.ID ? i1 : i2); Thanks Nick – Here’s the proof class Program { static void Main(string[] args) { IEnumerable<Item> items1 = new List<Item>() { new Item(){ ClientID = 1, ID = 1}, new Item(){ ClientID = 2, ID = 2}, new … Read more

Recursive control search with LINQ

Take the type/ID checking out of the recursion, so just have a “give me all the controls, recursively” method, e.g. public static IEnumerable<Control> GetAllControls(this Control parent) { foreach (Control control in parent.Controls) { yield return control; foreach(Control descendant in control.GetAllControls()) { yield return descendant; } } } That’s somewhat inefficient (in terms of creating lots … Read more

Sorting a list using Lambda/Linq to objects

This can be done as list.Sort( (emp1,emp2)=>emp1.FirstName.CompareTo(emp2.FirstName) ); The .NET framework is casting the lambda (emp1,emp2)=>int as a Comparer<Employee>. This has the advantage of being strongly typed. If you need the descending/reverse order invert the parameters. list.Sort( (emp1,emp2)=>emp2.FirstName.CompareTo(emp1.FirstName) );

Dynamic LINQ OrderBy on IEnumerable / IQueryable

Just stumbled into this oldie… To do this without the dynamic LINQ library, you just need the code as below. This covers most common scenarios including nested properties. To get it working with IEnumerable<T> you could add some wrapper methods that go via AsQueryable – but the code below is the core Expression logic needed. … Read more

techhipbettruvabetnorabahisbahis forumu