5
LINQ到對象中是否有等價的SQL IN語句?在LINQ到對象的聲明中
LINQ到對象中是否有等價的SQL IN語句?在LINQ到對象的聲明中
是 - 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);
}
(在LINQ to SQL中,最終爲 「IN」 查詢)。
注意在LINQ到對象上面是不是真的很有效。你會用更好的聯接:(這可能仍然可以用,當然點符號完成,但它最終被有些混亂)
var matches = from person in people
join name in desiredNames on person.FirstName equals name
select person;
我會去內蒙古加入這個上下文。如果我會使用contains
,它會重複6次,儘管事實上只有兩場比賽。我只是想在這裏強調一下,我會去參加聯盟而不是IN謂詞。
var desiredNames = new[] { "Pankaj" };
var people = new[]
{
new { FirstName="Pankaj", Surname="Garg" },
new { FirstName="Marc", Surname="Gravell" },
new { FirstName="Jeff", Surname="Atwood" }
};
var records = (from p in people join filtered in desiredNames on p.FirstName equals filtered select p.FirstName).ToList();
謝謝!你的聲譽在你之前;-) – 2009-01-08 11:12:01