C# LINQ 检查列表中是否存在 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8937826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:18:35  来源:igfitidea点击:

LINQ to check if ID exists in List

c#sqllinqentity-framework

提问by michael

I am using LINQ Entity framework. I have a SQL table and I want to get all the items in the table that have an ID that exist in a List

我正在使用 LINQ 实体框架。我有一个 SQL 表,我想获取表中具有列表中存在的 ID 的所有项目

Is this possible with LINQ?

这可以用 LINQ 实现吗?

采纳答案by Haris Hasan

Yes, it is possible.

对的,这是可能的。

(from item in yourContext.YourTable where yourList.Contains(item.ID) select item).ToList();

回答by Piotr Auguscik

You can do this with Containsits translated into sql IN:

您可以将Contains其转换为 sql来执行此操作IN

context.SomeTable.Where(r => someListOfId.Contains(r.ID));