Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq IN算子_C#_Linq To Sql - Fatal编程技术网

C# Linq IN算子

C# Linq IN算子,c#,linq-to-sql,C#,Linq To Sql,我试着寻找这个,但找不到适合我的情况的例子 我有这个方法来退货。如何使用代码的字符串数组对其进行过滤?包含对我不起作用 public static List<Customer> GetCustomers(string[] customerCodesArray) { using (busDataContext g = new busDataContext()) { return g.Customers.Where( x =>

我试着寻找这个,但找不到适合我的情况的例子

我有这个方法来退货。如何使用代码的字符串数组对其进行过滤?包含对我不起作用

public static List<Customer> GetCustomers(string[] customerCodesArray)
{
    using (busDataContext g = new busDataContext())
    {
        return g.Customers.Where(
            x => x.customerCode.Contains(customerCodesArray)).ToList();
    }
}
公共静态列表GetCustomers(字符串[]customerCodesArray) { 使用(busDataContext g=new busDataContext()) { 返回g.Customers.Where( x=>x.customerCode.Contains(customerCodesArray)).ToList(); } } 您正在后退:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();

我认为您需要反转
Contains
表达式,因为您希望查看数组是否包含客户代码,而不是相反

试试这个:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();

我想这就是你想要的

    return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();
试一试


请尝试以下代码:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList(); 

请澄清“到底什么”不起作用……你所说的“不起作用”是什么意思?你有编译错误吗?如果是,错误是什么?或者你会遇到运行时错误吗?如果是,错误是什么?虽然技术上正确,但在使用SQL时,这并不是性能最佳的解决方案。它需要扫描整个Customers表,最可能的情况是应用customer codes数组中的每条记录。其复杂性变为O(N*M)。使用Linq join语句更为实用,因此生成的SQL查询也可以使用完全依赖于数据库索引的join元素。它不一定会生成扫描(如果生成了扫描,连接也不会更好)。根据我的经验,只有当
customerCodesArray
包含数千项时,插入临时表并加入才能提供性能优势。
return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();