编辑得更清楚。
例如,如果我有这个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
};
答案 0 :(得分:2)
要将条件与OR而不是AND组合,请使用PredicateBuilder:
predicate = predicate.Or(p => p.Description.Contains (temp));
return dataContext.Products.Where(predicate);
答案 1 :(得分:1)
你不能通过链接来实现 - 你必须使用某种表达式构建器,或者只是在原始过滤器中构建它:
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
};