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

时间:2013-11-02 09:25:07

标签: c# linq

运营商应该是'AND'而不是'OR'。

我正在尝试重构以下代码,我理解以下编写linq查询的方式可能不是正确的方法。可以告诉我如何将以下内容组合成一个查询。

        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();
            }

3 个答案:

答案 0 :(得分:2)

首先,除非AllCompany属于某种神奇的自定义类型,否则第一行不会给你任何东西。 我也怀疑Distinct是否按照你想要的方式工作。我不知道AllCompany的类型,但我猜它只给你参考区别。

无论哪种方式,我想你想要的东西:

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();

修改

我将Distinct移到了查询的末尾以优化处理。

答案 1 :(得分:0)

这样尝试怎么样;

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

如果查询的每个部分都是可选的(如创建日期,最后更新日期......),那么您可以构建linq查询字符串。

答案 2 :(得分:0)

这是一个偷偷摸摸的伎俩。如果在其自己的静态类中定义以下扩展方法:

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

然后你可以写

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();