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

This question already has answers here: How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate] (9 answers) How to use LINQ to select object with minimum or maximum property value (19 answers) Closed 6 years ago. If I have a class that looks … Read more

Learning about LINQ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 3 years ago. Improve this question Overview One of the things I’ve … Read more

Find() vs. Where().FirstOrDefault()

I often see people using Where.FirstOrDefault() to do a search and grab the first element. Why not just use Find()? Is there an advantage to the other? I couldn’t tell a difference. namespace LinqFindVsWhere { class Program { static void Main(string[] args) { List<string> list = new List<string>(); list.AddRange(new string[] { “item1”, “item2”, “item3”, “item4” … Read more

Linq select objects in list where exists IN (A,B,C)

I have a list of orders. I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in (“A”, “B”, “C”) // Filter the orders based on the order status var filteredOrders = from order in orders.Order where order.StatusCode.????????(“A”, “B”, “C”) select order; 5 Answers 5

Code equivalent to the ‘let’ keyword in chained LINQ extension method calls

Using the C# compilers query comprehension features, you can write code like: var names = new string[] { “Dog”, “Cat”, “Giraffe”, “Monkey”, “Tortoise” }; var result = from animalName in names let nameLength = animalName.Length where nameLength > 3 orderby nameLength select animalName; In the query expression above, the let keyword allows a value to … Read more

Dynamic LINQ OrderBy on IEnumerable / IQueryable

I found an example in the VS2008 Examples for Dynamic LINQ that allows you to use a SQL-like string (e.g. OrderBy(“Name, Age DESC”)) for ordering. Unfortunately, the method included only works on IQueryable<T>. Is there any way to get this functionality on IEnumerable<T>? 22 s 22 Just stumbled into this oldie… To do this without … Read more