使用 OR 而不是 AND 链接/构建 LINQ 查询

Chaining / building LINQ query with an OR instead of AND

编辑得更清楚。

如果我有这个 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,因为它本质上是 LinqKit,它具有 EntityFramework (≥ 6.0.2) 的依赖项,我卡住了使用 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
            };

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

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

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

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