c# - 如何使用多参数linq从数据库中获取记录

标签 c# .net linq

我有一个从数据库中获取记录的查询, 我需要在参数中传递 ids 来调用这个函数, 像 Myfunction(1, 2, 3)

我想得到与这个id匹配的结果,

喜欢

public List<Items> GetItems(int[] ids)
{
var a = from Items in db.item 
where items.id == ids[]
select new Items
{

}
    return a.ToList();
}

在 where 子句上存在混淆(如何获取有关提供的记录)

我试过循环 id 但没有成功

提前致谢

最佳答案

您可以使用Contains, 方法来检查。类似于 Select * from table where IDs in (1,2,3,...)

尝试以下操作。

var a = from item in db.Items
        where ids.Contains(item.id)
        select new Item {.....}

ids 是您的数组。

您可能会看到:Creating IN Queries With Linq To Sql

关于c# - 如何使用多参数linq从数据库中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13025687/

相关文章:

c# - 表单验证 : Where’s my auth cookie gone?

c# - DataContractSerializer 中缺失数据成员的默认值

c# - iTextSharp 创建页脚页 # of#

c# - LINQ to 对象基础

c# - 如何在 IEnumerable 的末尾添加更多查询

c# - 合并 Linq 查询

java - C# 中 LinkedHashSet (Java) 的等价物是什么?

C#/.NET,看什么?

c# - 防止 visual studio 将 setter 方法限制为内部

c# - EF6 : Configure complex mapping for entities (code first)