【发布时间】: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