gpt4 book ai didi

linq - LINQ to objects 的 In 语句

转载 作者:行者123 更新时间:2023-12-04 16:37:59 25 4
gpt4 key购买 nike

LINQ to objects 中是否有等效的 SQL IN 语句?

最佳答案

是 - 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 to Objects 中,上述内容并不是很有效。你最好加入:
var matches = from person in people
join name in desiredNames on person.FirstName equals name
select person;

(当然,这仍然可以用点表示法来完成,但最终会有些困惑。)

关于linq - LINQ to objects 的 In 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/423840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com