【问题标题】:Chaining / building LINQ query with an OR instead of AND使用 OR 而不是 AND 链接/构建 LINQ 查询
【发布时间】:2015-09-30 12:28:19
【问题描述】:

编辑得更清楚。

例如,如果我有这个 IQueryable:

DateTime theDate = new DateTime(2015, 09, 30);

var query = from org in Organisations
            where org.DisableOrganisation == false &&
              org.DisableReports == false
            select new
            {
                OrganisationId = org.OrganisationId
            };

稍后在方法中我想添加一个 OR 到它,例如

// Check to see if the date is the last day of the month.
if (theDate.AddDays(1).Day == 1)
{
    // The following statement introduces an AND but I want to make this an OR.
    query = query.Where(x => x.Frequency == "M");
}

这有效地使我的查询...

var query = from org in Organisations
            where org.DisableOrganisation == false &&
              org.DisableReports == false &&
              org.Frequency == "M"
            select new
            {
                OrganisationId = org.OrganisationId
            };

但我想成功...

var query = from org in Organisations
            where (org.DisableOrganisation == false &&
              org.DisableReports == false) ||
              org.Frequency == "M"
            select new
            {
                OrganisationId = org.OrganisationId
            };

如何将其设为 OR 而不是 AND ?

P.S. 不能使用 PredicateBuilder,因为它本质上是具有 EntityFramework (≥ 6.0.2) 依赖项的 LinqKit,我无法使用 EF 4.3.1

已解决:感谢 D Stanley(老实说,我以前使用过这种形式的解决方案,我只是忘记了它)。

DateTime theDate = new DateTime(2015, 09, 30);
bool isEndOfMonth = theDate.AddDays(1).Day == 1;

var query = from org in Organisations
            where (org.DisableOrganisation == false &&
              org.DisableReports == false) ||
              (isEndOfMonth &&
               pr.ProfileType == "L" && 
               pr.Frequency == "M")
            select new
            {
                OrganisationId = org.OrganisationId
            };

【问题讨论】:

  • LINQ WHERE with OR 的可能重复项
  • @TomDoesCode 它本质上重复了那个问题,但是那个问题已经有 4 年历史了……有更新的解决方案吗?
  • @TomDoesCode 可能的重复点指向LinqKit ...它依赖于EntityFramework(≥6.0.2),我使用的是EF 4.3.1

标签: c# linq linq-to-entities


【解决方案1】:

要使用 OR 而不是 AND 组合条件,请使用 PredicateBuilder:

predicate = predicate.Or(p => p.Description.Contains (temp));
return dataContext.Products.Where(predicate);

【讨论】:

  • LinqKit... 依赖 EntityFramework (≥ 6.0.2),我使用的是 EF 4.3.1
  • 源代码是开放的,也许可以提取你需要的功能。 github.com/scottksmith95/LINQKit
【解决方案2】:

你不能通过链接来做到这一点 - 你必须使用某种表达式构建器,或者只是在你的原始过滤器中构建它:

var day = theDate.AddDays(1).Day;

var query = from org in Organisations
            where (org.DisableOrganisation == false &&
                   org.DisableReports == false) ||
                  (day == 1 &&
                   org.ProfileType == "L" && 
                   org.Frequency == "M")
            select new
            {
                OrganisationId = org.OrganisationId
            };

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2014-05-11
    相关资源
    最近更新 更多