Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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中使用multiple WHERE谓词比使用multiple AND更慢?_C#_.net_Linq_Visual Studio - Fatal编程技术网

C# 为什么在LINQ中使用multiple WHERE谓词比使用multiple AND更慢?

C# 为什么在LINQ中使用multiple WHERE谓词比使用multiple AND更慢?,c#,.net,linq,visual-studio,C#,.net,Linq,Visual Studio,通常使用LINQ来过滤对象数组 我运行了一个产生相同结果的测试表达式,但由于时间不同,我想知道这种行为的原因 public long testTimeOperetionWHERE() { Object[] list = opCoIn.getList(); System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); int i = 0; while (i<20000)

通常使用LINQ来过滤对象数组

我运行了一个产生相同结果的测试表达式,但由于时间不同,我想知道这种行为的原因

public long testTimeOperetionWHERE()
{
    Object[] list = opCoIn.getList();
    System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();


    int i = 0;
    while (i<20000)
    {
        var result = list.Where(o => o.Id>0)
                     .Where(o => o.Import>0)
                     .Where(o => o.OrderConfirm==o.NumberConfirm)
                     .Where(o => o.IdActiveCustomer>100 );
        i++;
    }

    long e = sw.ElapsedMilliseconds; 

    return e;
}
public long testtimeoperationwhere()
{
Object[]list=opCoIn.getList();
System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
int i=0;
而(IO.Id>0)
。其中(o=>o.Import>0)
.其中(o=>o.OrderConfirm==o.NumberConfirm)
其中(o=>o.IdActiveCustomer>100);
i++;
}
长e=sw.ElapsedMilliseconds;
返回e;
}
时间成本结果始终在90-80之间变化

在这种情况下

public long testTimeOperetionAND()
{
    Object[] list = opCoIn.getList();
    System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();


    int i = 0;
    while (i < 20000)
    {
        var result = list.Where(o => o.Id > 0
                                && o.Import > 0
                                && o.OrderConfirm==o.NumberConfirm
                                && o.IdActiveCustomer>100);

        i++;
    }

    long e = sw.ElapsedMilliseconds; 

    return e;
}
public long testtimeoperationand()
{
Object[]list=opCoIn.getList();
System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
int i=0;
而(i<20000)
{
var result=list.Where(o=>o.Id>0
&&o.导入>0
&&o.OrderConfirm==o.NumberConfirm
&&o.IdActiveCustomer>100);
i++;
}
长e=sw.ElapsedMilliseconds;
返回e;
}

时间开销始终小于5

您甚至没有执行此LINQ查询。你只是在定义它

例如,如果您使用
foreach
ToList()
Count()
,您将获得更有意义的结果

while (i < 20000)
{
    var result = list.Where(o => o.Id > 0
                            && o.Import > 0
                            && o.OrderConfirm==o.NumberConfirm
                            && o.IdActiveCustomer>100); // not executed
    int justToExecuteIt = result.Count();               // executed here
    i++;
}
while(i<20000)
{
var result=list.Where(o=>o.Id>0
&&o.导入>0
&&o.OrderConfirm==o.NumberConfirm
&&o.IdActiveCustomer>100);//未执行
int justToExecuteIt=result.Count();//在此处执行
i++;
}
连续的
Where
s和连续的
&
之间应该没有太大的区别

我最近提出了一个类似的问题:


您可能还发现,这个问题对于理解LINQ延迟执行的好处很有用:

在任何时候您都不会实际执行查询。您要衡量的是调用一次方法还是调用四次方法更快。我一点也不奇怪一次的速度更快。我有点惊讶。我以为LINQ会更快地调用自己4倍。愚蠢的林奇不确定,只是问。方法之间的差异是否取决于提供程序返回的是
IQueryable
还是
IEnumerable
?在第一种情况下,多个
Where
子句只会修改语法树,但在第二种情况下,它们会导致集合的额外枚举,这可能是显而易见的。如果我错了,请纠正我。UPD.:噢,我看到Marc Gravell在你的链接上准确地回答了这个问题。@AndreySarafanov:即使在LINQ中,由于延迟执行,多个
的对象也不会导致多个枚举。这就是我上面的问题。顺便说一句,为什么投反对票?明白了,谢谢。多个枚举数和委托调用所造成的开销确实很小。我很抱歉给你的答案打了个负号,因为我对这个问题的理解有误。@AndreySarafanov:没问题!但如果你愿意,你可以撤销向上/向下的投票。再点击一次。+1,很好的解释,蒂姆,仍然不确定是否会投反对票。