Finding user that have at least one tag object in linq?
I have an object User.
User 1..N Tags(string).
For instance, i have 开发者_C百科a List of Tag object. How can i query the User to find all the user that have at least 1 Tag in side the list of tags?
Thanks in advance :)
Assuming your code snippet means you have a property Tags
of type Foo<string>
where Foo
is some sequence type, you could just use:
var taggedUsers = users.Where(user => tags.Any(tag => user.Tags.Contains(tag));
(Actually, that's assuming you have a list of strings as Tags
- the question is somewhat unclear. However, hopefully it'll be enough to sort you out.)
EDIT: Okay, with the details in the comments, I think you probably just need:
var taggedUsers = users.Where(user => tags.Any(tag => user.Tags
.Select(t => t.Value)
.Contains(tag));
精彩评论