LINQ:有多个条件和一个条件连续何在凡单之间的差异 [英] LINQ: differences between single Where with multiple conditions and consecutive Wheres with single condition

查看:292
本文介绍了LINQ:有多个条件和一个条件连续何在凡单之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有串联多个任何不利其中,在LINQ而不是使用一个单一的其中,与多个条件<? / p>

我这么问是因为使用多个其中,可帮助降低复杂性并提高我的代码相当可维护性。



考虑下面的代码, chargeList 列表<电荷> 这是一个的BindingSource 来源:

 的IEnumerable<电荷> matchingCharges = chargeList; 
如果
matchingCharges = matchingCharges
。凡(C => c.ChannelAbbreviation == channelAbbr)(string.IsNullOrWhiteSpace(channelAbbr)!);
如果(deliveryNoteDate.HasValue)
matchingCharges = matchingCharges
。凡(C => c.ArrivalAt == deliveryNoteDate.Value);
如果(chargeID.HasValue)
matchingCharges = matchingCharges
。凡(C => c.ChargeID == chargeID.Value);

这简洁的代码将处理过滤器,无,一,二,所有的所有组合。



否则我不得不使用的if-else 和一个其中,



这是我脑海最佳:

  //重要的是保持代码的可读性:
布尔filterChannel = string.IsNullOrWhiteSpace(channelAbbr);
布尔filterDate = deliveryNoteDate.HasValue;
布尔filterID = chargeID.HasValue;

如果(filterChannel&安培;!&安培;!filterDate和放大器;&安培;!filterID)
{
//采取一切
matchingCharges = chargeList;
}
,否则
{
matchingCharges = chargeList
。凡(C =>
filterChannel c.ChannelAbbreviation == channelAbbr:真正的
和;&安培; filterDate c.ArrivalAt == deliveryNoteDate.Value:真正的
和;&安培; filterID c.ChargeID == chargeID.Value:TRUE);
}



那么,什么都之间的差异,是他们可以忽略不计?是否LINQ提供程序回事?


解决方案

语义,存在的情况下没有什么区别其中,(相比之下排序依据,它需要更多的照顾)。在执行层面,它只是多个谓词用简单的表达式树,而不是一个复杂的表达式树一个谓语;但大多数发动机将应付罚款无论是。



有关你在做什么,多个其中,是理想的。


Is there any disadvantage in concatenating multiple Where in LINQ instead of using a single Where with multiple conditions?

I'm asking because using multiple Where can help to reduce complexity and improve maintainability of my code considerably.

Consider following code, chargeList is a List<Charge> which is the source of a 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);

This concise code will handle all combinations of filter, none,one,two,all.

Otherwise i'd have to use if-else and multiple conditions in a single Where.

This is the best that comes to my mind:

// important to keep code readable:
bool filterChannel = !string.IsNullOrWhiteSpace(channelAbbr);
bool filterDate = deliveryNoteDate.HasValue;
bool filterID = chargeID.HasValue;

if(!filterChannel && !filterDate && !filterID)
{
    // take all 
    matchingCharges = chargeList;
}
else
{
    matchingCharges = chargeList
        .Where(c => 
            filterChannel ? c.ChannelAbbreviation == channelAbbr : true
            && filterDate ? c.ArrivalAt == deliveryNoteDate.Value : true
            && filterID   ? c.ChargeID ==  chargeID.Value : true);
}

So what are the differences between both, are they negligible? Does the LINQ provider matter?

解决方案

Semantically, there is no difference in the case of Where (contrast OrderBy, which needs more care). At the implementation level, it is simply multiple predicates with simple expression trees instead of a single predicate with a complex expression tree; but most engines will cope fine with either.

For what you are doing, multiple Where is ideal.

这篇关于LINQ:有多个条件和一个条件连续何在凡单之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆