开发者

How to order by a property in a collection using linq?

开发者 https://www.devze.com 2022-12-31 20:16 出处:网络
开发者_C百科I have a link sequence, and I want to order by User.Name (its a sequence of users). How can I do that?
开发者_C百科

I have a link sequence, and I want to order by User.Name (its a sequence of users).

How can I do that?

Also, if I want to remove any users with User.Count = 0 can I do that in the same query?


IEnumerable<User> result = from user in users
                           where user.Count != 0
                           orderby user.Name
                           select user;

or

IEnumerable<User> result = users.Where(user => user.Count != 0)
                                .OrderBy(user => user.Name);

where users is a IEnumerable<User> (such as a List<User>).

This selects all users where user.Count != 0 and returns them ordered by user.Name.

Note that the original collection users remains unchanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消