简体   繁体   English

避免使用linq多次浏览列表,具有动态条件(过滤器)

[英]Avoid to browse a list multiple time with linq, with dynamic conditions (filter)

I have a list with Article objects. 我有一个包含Article对象的列表。 Parameters for this object are 该对象的参数是

Category, Description, Status, Class 类别,描述,状态,类

The user can filter the list with the combinaison he wants. 用户可以使用他想要的组合过滤列表。 Only Description, or Category + Class, an so on. 仅描述,或类别+类,依此类推。

So if the user choose a criterion i'll get an int >= 0, otherwise i'll get -1. 因此,如果用户选择一个标准,我将得到一个int> = 0,否则我将得到-1。

For example if he choose to filter with Status and Category , i'll get 例如,如果他选择过滤StatusCategory ,我会得到

FilterCategory = x, FilterDescription = -1, FilterStatus = y, FilterClass = -1 FilterCategory = x,FilterDescription = -1,FilterStatus = y,FilterClass = -1

My way to filter the list is the following: 我过滤列表的方法如下:

if (FilterCategory != -1)
    list = list.Where(a => a.Category == FilterCategory);
if (FilterDescription != -1)
    list = list.Where(a => a.Description == FilterDescription);
if (FilterStatus != -1)
    list = list.Where(a => a.Status == FilterStatus);
if (FilterClass != -1)
    list = list.Where(a => a.Class == FilterClass);

In this way, i have to iterate the list 4 time, it's not efficient with a lot of items. 通过这种方式,我必须迭代列表4次,它对很多项目效率不高。 I would browse the list once, and check the 4 condition in an unique where. 我会浏览一次列表,并在一个独特的位置检查4条件。 But i don't know how to do that with the specific condition != -1. 但我不知道具体情况如何做到这一点!= -1。

Thank you 谢谢

As Juharr mentions in the comments, Linq will not evaluate your .Where right after calling it. 作为Juharr在评论中提到,LINQ的不会评价你的.Where调用它之后。 Only when calling ToList/ToArray/First/Single/foreach will it evaluate the clause and it will automatically combine your filters. 只有在调用ToList/ToArray/First/Single/foreach时,它才会评估该子句,它会自动组合您的过滤器。

You can do a HUGE Where query moving all your conditions it a single Where by noticing that: 你可以做一个巨大的地方查询将所有条件移动到一个Where通过注意:

if (FilterCategory != -1)
    list = list.Where(a => a.Category == FilterCategory);

is equivalent to: 相当于:

list = list.Where(a => FilterCategory == -1 || a.Category == FilterCategory);

Then the query is: 然后查询是:

list = list.Where(a => (FilterCategory == -1 || a.Category == FilterCategory)
                    && (FilterDescription == -1 || a.Description == FilterDescription)
                    && (FilterStatus == -1 || a.Status == FilterStatus)
                    && (FilterClass == -1 || a.Class == FilterClass));

But I really doubt it will improve your enumeration performance. 但我真的怀疑它会提高你的枚举性能。

list = list.Where(a => (FilterCategory != -1 || a.Category == FilterCategory) && 
                                       (FilterDescription != -1 || a.Description == FilterDescription) &&
                                       (FilterStatus != -1 || a.Status == FilterStatus) &&
                                       (FilterClass != -1 || a.Class == FilterClass));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM