Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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:具有多个条件的单个Where与具有单个条件的连续Where之间的差异_C#_Linq - Fatal编程技术网

C# LINQ:具有多个条件的单个Where与具有单个条件的连续Where之间的差异

C# LINQ:具有多个条件的单个Where与具有单个条件的连续Where之间的差异,c#,linq,C#,Linq,在LINQ中连接多个Where而不是使用一个带多个条件的Where是否有任何缺点 我这样问是因为在其中使用多个,可以大大降低代码的复杂性并提高代码的可维护性 考虑以下代码,chargeList是一个List,它是BindingSource的源: IEnumerable<Charge> matchingCharges = chargeList; if(!string.IsNullOrWhiteSpace(channelAbbr)) matchingCharges = match

在LINQ中连接多个
Where
而不是使用一个带多个条件的
Where
是否有任何缺点

我这样问是因为在其中使用多个
,可以大大降低代码的复杂性并提高代码的可维护性

考虑以下代码,
chargeList
是一个
List
,它是
BindingSource
的源:

IEnumerable<Charge> matchingCharges = chargeList;
if(!string.IsNullOrWhiteSpace(channelAbbr))
    matchingCharges = matchingCharges
        .Where(c => c.ChannelAbbreviation == channelAbbr);
if(deliveryNoteDate.HasValue)
    matchingCharges = matchingCharges
        .Where(c => c.ArrivalAt == deliveryNoteDate.Value);
if(chargeID.HasValue)
    matchingCharges = matchingCharges
        .Where(c => c.ChargeID == chargeID.Value);

那么两者之间的区别是什么,它们可以忽略不计吗?LINQ提供者重要吗

语义上,
Where
(对比
OrderBy
,需要更多注意)的情况没有区别。在实现层面上,它只是具有简单表达式树的多个谓词,而不是具有复杂表达式树的单个谓词;但大多数引擎都能很好地应对这两种情况


对于你正在做的事情,多个
,其中
是理想的。

我也在想同样的事情。这就是为什么我在自己的应用程序中尝试了这个

我有一个包含大量条目的列表,这是我尝试的:

//TEST 1
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var hoursLinq = _hourDataSource.Hours
            .Where(hour => hour.Id == profile.Id)
            .Where(hour => hour.DayName.Equals("Maandag"))
            .Where(hour => hour.Day == 1)
            .Select(hour => hour);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts1 = stopWatch.Elapsed;

//TEST 2
stopWatch = new Stopwatch();
stopWatch.Start();
var hoursLinq2 = _hourDataSource.Hours
            .Where(hour => hour.Id == profile.Id)
            .Select(hour => hour);

if (hoursLinq2.Count() != 0)
{
    var hoursLinq3 = _hourDataSource.Hours
            .Where(hour => hour.DayName.Equals("Maandag"))
            .Select(hour => hour);

    if (hoursLinq3.Count() != 0)
    {
        var hoursLinq4 = _hourDataSource.Hours
            .Where(hour => hour.Day == 1)
            .Select(hour => hour);
    }
}

stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts2 = stopWatch.Elapsed;

//TEST 3
stopWatch = new Stopwatch();
stopWatch.Start();
var hoursLinq5 = _hourDataSource.Hours
            .Where(hour => hour.Id == profile.Id &&
                            hour.DayName.Equals("Maandag") &&
                            hour.Day == 1)
            .Select(hour => hour);

stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts3 = stopWatch.Elapsed;
每个时间跨度(ts1、ts2、ts3)在经过的时间上都有很小的差异,我很确定您可以忽略它


我想这是我个人的偏好,我喜欢多个where's,因为它的可读性

据我所知,多个where's是推荐的approach@VladimirSachek您建议的编辑最好是作为注释(这里是链接:)而不是带有复杂表达式树的单个谓词。请注意,如果您使用LINQ对象,多个
Where
语句的谓词将组合成一个谓词,并且无论链接在一起的
Where
子句的数量如何,只调用一个
foreach
循环。@sloth,这有点取决于其他情况;但是是的,在许多(并非所有)情况下,
迭代器。Where(…)
方法将用于组合它们,减少循环。@sloth您能提供谓词组合部分的源代码吗?@sgarg当然,请看一看。在
WhereEnumerableIterator
上调用
WhereEnumerableIterator
(例如,通过组合
Where
调用)将导致使用单个
迭代器
,并且所有谓词都与
和&
简单地“组合”
//TEST 1
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var hoursLinq = _hourDataSource.Hours
            .Where(hour => hour.Id == profile.Id)
            .Where(hour => hour.DayName.Equals("Maandag"))
            .Where(hour => hour.Day == 1)
            .Select(hour => hour);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts1 = stopWatch.Elapsed;

//TEST 2
stopWatch = new Stopwatch();
stopWatch.Start();
var hoursLinq2 = _hourDataSource.Hours
            .Where(hour => hour.Id == profile.Id)
            .Select(hour => hour);

if (hoursLinq2.Count() != 0)
{
    var hoursLinq3 = _hourDataSource.Hours
            .Where(hour => hour.DayName.Equals("Maandag"))
            .Select(hour => hour);

    if (hoursLinq3.Count() != 0)
    {
        var hoursLinq4 = _hourDataSource.Hours
            .Where(hour => hour.Day == 1)
            .Select(hour => hour);
    }
}

stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts2 = stopWatch.Elapsed;

//TEST 3
stopWatch = new Stopwatch();
stopWatch.Start();
var hoursLinq5 = _hourDataSource.Hours
            .Where(hour => hour.Id == profile.Id &&
                            hour.DayName.Equals("Maandag") &&
                            hour.Day == 1)
            .Select(hour => hour);

stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts3 = stopWatch.Elapsed;