在LINQ对象中是否有等效的SQL IN语句?
是的 - 包含.
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); }
(在LINQ to SQL中,这最终成为"IN"查询.)
请注意,在LINQ to Objects中,上面的效率并不高.加入你会更好:
var matches = from person in people join name in desiredNames on person.FirstName equals name select person;
(当然,这仍然可以通过点符号来完成,但最终会有些麻烦.)