开发者

Select Items from List<object> by conitaining List<guid> in C#

I am having the ListAllUsers . User having propeorty id(as Guid),na开发者_开发百科me(as String),address(as String). I am having another List ids of users id which are selected from ui. I want to get List selectedUsers, where ids from AllUsers. I am trying to fetch it by LINQ. can i do this by single statement , without multiple itterations? how?


If I understand your question correctly, you can do something like:

var selectedUsers = allUsers.Where(user => selectedUserIds.Contains(user.Id));


Another possible approach is:

var selectedUsers = selectedUserIds.Select(id => allUsers.First(user => user.id == id))

This will throw an exception if one of the selectedUserIds doesn't map to a user which might be what you want. The method by Frédéric will silently swallow.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜