Reputation: 6640
To keep things simple, I have this class:
public class Contact
{
public string Name { get; set; }
public string[] Emails { get; set; }
}
I have a collection of contacts = IEnumerable<Contact>
I need to find all contacts in that collection that have, let's say a text "xxx" in their email addresses (they may have multiple emails).
Something like that doesn't work of course:
var found = contacts.Where(c => c.Emails.Where(e => e.Contains("xxx")));
I am wondering how to build such query using lambda expression?
Thanks.
Upvotes: 2
Views: 391
Reputation: 174289
Use Any
instead of Where
in the inner expression:
var found = contacts.Where(c => c.Emails.Any(e => e.Contains("xxx")));
Upvotes: 4
Reputation: 14282
Try this
var found = contacts.Where(c => c.Emails.Where(e => e.Contains("xxx")).Count() > 0);
This will return all the contacts according to the specified email condition.
Good Luck !!
Upvotes: 0