使用列表创建 linq 条件

Creating a linq condition using a list

我有一个用于从数据库中获取列表的方法。

public List<SelectedCustomers> GetCustomers(List<int> customerNumbers)
{
   var customers=_context.Customers.Where(?).Select(i=> new SelectedCustomers() {}).ToList()
}

我想从用户给定客户编号的客户数据库中检索信息。数据库中的客户列表中大约有十万个客户。我不希望该方法在每次调用时都获取整个列表并进行搜索,这太费力了。但是,我不知道如何在 where ().

中使用列表

总而言之,我不想拉出我想要的所有列表并在列表中搜索用户请求的值,我想使用直接来自用户的列表转到数据库并给我信息这些客户。

我希望我能解释一下。感谢您的帮助。

试试这个:

var customers = _context.Customers.Where(c => customerNumber.Contains(c.CustomerId)).Select(i => new SelectedCustomers() { }).ToList()

尝试类似(未测试)

public List<SelectedCustomers> GetCustomers(List<int> customerNumbers)
{
   var customers=_context.Customers.Where(x =customerNumbers.Contains(x.customerNumber)
                       .Select(i=> new SelectedCustomers() {}).ToList()
}

这相当于 SQL IN ()