简体   繁体   English

如何将多个部分linq组合成一个查询?

[英]How to combine the multiple part linq into one query?

Operator should be 'AND' and not a 'OR'. 运算符应为“AND”而不是“OR”。

I am trying to refactor the following code and i understood the following way of writing linq query may not be the correct way. 我试图重构以下代码,我理解以下编写linq查询的方式可能不是正确的方法。 Can somone advice me how to combine the following into one query. 可以告诉我如何将以下内容组合成一个查询。

  AllCompany.Where(itm => itm != null).Distinct().ToList(); if (AllCompany.Count > 0) { //COMPANY NAME if (isfldCompanyName) { AllCompany = AllCompany.Where(company => company["Company Name"].StartsWith(fldCompanyName)).ToList(); } //SECTOR if (isfldSector) { AllCompany = AllCompany.Where(company => fldSector.Intersect(company["Sectors"].Split('|')).Any()).ToList(); } //LOCATION if (isfldLocation) { AllCompany = AllCompany.Where(company => fldLocation.Intersect(company["Location"].Split('|')).Any()).ToList(); } //CREATED DATE if (isfldcreatedDate) { AllCompany = AllCompany.Where(company => company.Statistics.Created >= createdDate).ToList(); } //LAST UPDATED DATE if (isfldUpdatedDate) { AllCompany = AllCompany.Where(company => company.Statistics.Updated >= updatedDate).ToList(); } //Allow Placements if (isfldEmployerLevel) { fldEmployerLevel = (fldEmployerLevel == "Yes") ? "1" : ""; AllCompany = AllCompany.Where(company => company["Allow Placements"].ToString() == fldEmployerLevel).ToList(); } 

Firstly, unless AllCompany is of some magic custom type, the first line gives you nothing. 首先,除非AllCompany属于某种神奇的自定义类型,否则第一行不会给你任何东西。 Also I have a doubt that Distinct works the way You want it to. 另外,我怀疑Distinct运作方式与你想要的一样。 I don't know the type of AllCompany but I would guess it gives you only reference distinction. 我不知道AllCompany的类型,但我猜它只给你参考区别。

Either way here'w what I think You want: 无论哪种方式,我想你想要的:

fldEmployerLevel = (fldEmployerLevel == "Yes") ? "1" : "";

var result = AllCompany.Where(itm => itm != null)
    .Where(company => !isfldCompanyName || company["Company Name"].StartsWith(fldCompanyName))
    .Where(company => !isfldSector|| fldSector.Intersect(company["Sectors"].Split('|')).Any())
    .Where(company => !isfldLocation|| fldLocation.Intersect(company["Location"].Split('|')).Any())
    .Where(company => !isfldcreatedDate|| company.Statistics.Created >= createdDate)
    .Where(company => !isfldUpdatedDate|| company.Statistics.Updated >= updatedDate)
    .Where(company => !isfldEmployerLevel|| company["Allow Placements"].ToString() == fldEmployerLevel)
    .Distinct()
    .ToList();

Edit: 编辑:

I moved Distinct to the end of the query to optimize the processing. 我将Distinct移到查询的末尾以优化处理。

How about trying like this; 怎么样这样做;

AllCompany = AllCompany .Where(company => (company => company.Statistics.Created >= createdDate)) && (company.Statistics.Updated >= updatedDate));

If every part of query is optional (like created date, last update date..) then you can build linq query string. 如果查询的每个部分都是可选的(如创建日期,最后更新日期..),那么您可以构建linq查询字符串。

Here's a sneaky trick. 这是一个偷偷摸摸的伎俩。 If you define the following extension method in its own static class: 如果在其自己的静态类中定义以下扩展方法:

public virtual IEnumerable<T> WhereAll(params Expression<Predicate<T> filters)
{
    return filters.Aggregate(dbSet, (acc, element) => acc.Where(element));
}

then you can write 然后你可以写

var result = AllCompany.WhereAll(itm => itm != null,
    company => !isfldCompanyName || company["Company Name"].StartsWith(fldCompanyName),
    company => !isfldSectorn || fldSector.Intersect(company["Sectors"].Split('|')).Any(),
    company => !isfldLocation || fldLocation.Intersect(company["Location"].Split('|')).Any(),
    company => !isfldcreatedDate || company.Statistics.Created >= createdDate,
    company => !isfldUpdatedDate || company.Statistics.Updated >= updatedDate,
    company => !isfldEmployerLevel || company["Allow Placements"].ToString() == fldEmployerLevel)
    .Distinct()
    .ToList();

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

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