0

运算符应该是“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();
            }
4

3 回答 3

2

首先,除非AllCompany是某种神奇的自定义类型,否则第一行什么也没有。我也怀疑它Distinct的工作方式是你想要的。我不知道的类型,AllCompany但我想它只给你参考区别。

无论哪种方式here'w我认为你想要的:

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查询的末尾以优化处理。

于 2013-11-02T09:48:22.073 回答
0

像这样尝试怎么样?

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

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

于 2013-11-02T09:41:55.703 回答
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();
于 2013-11-02T09:56:45.487 回答