编辑得更清楚。
例如,如果我有这个 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
SOLVED:感谢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
};