Yes - Contains .
var desiredNames = new[] { "Jon", "Marc" };
var people = new[]
{
new { FirstName="Jon", Surname="Skeet" },
new { FirstName="Marc", Surname="Gravell" },
new { FirstName="Jeff", Surname="Atwood" }
};
var matches = people.Where(person => desiredNames.Contains(person.FirstName));
foreach (var person in matches)
{
Console.WriteLine(person);
}
(In LINQ to SQL, this ends up as an "IN" query.)
Note that in LINQ to Objects this is not very efficient. You will be better off connecting:
var matches = from person in people
join name in desiredNames on person.FirstName equals name
select person;
(This can still be done with dotted notation, of course, but it's ultimately a bit messier.)
source
share