简体   繁体   中英

Using Linq, order objects by some property and select the first 2 objects

Using a linq query/method chainging I wish to select just the first 2 Point objects in a List ordered by Point.X. How can I?

myList.OrderBy(item => item.X).Take(2);

Breaking it down:

OrderBy() takes a lambda expression which selects the key by which to order. In this case, we want to return the .X property on the object. Another example is if we had a Person object and wanted to sort by .FirstName , the key selector would be (item => item.FirstName) .

Take() truncates the enumeration to the specified number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM