[英]LINQ where not between a date range
我有以下linq查询:
public static int GetContributions(PERSON person, DateTime startDate, DateTime endDate)
{
using (var db = new TestEntities())
{
var creditsSum = (from u in db.PERSON_SOCIAL_INSURANCE_CONTRIBUTIONS
where u.StartDate >= startDate
where u.EndDate <= endDate
where (u.PersonId == person.Id)
select (int?)u.NumOfContributions).Sum() ?? 0;
return creditsSum;
}
}
我想创建一个类似的方法,以返回不位于提供的开始日期和结束日期之间的捐款数量。 因此,基本上,它返回不属于startDate和endDate值之间的所有条目。
有什么帮助吗?
然后将所有条件组合在一起,在它们之间加上括号,然后取反。
var creditsSum = (from u in db.PERSON_SOCIAL_INSURANCE_CONTRIBUTIONS
where !(u.StartDate >= startDate
&& u.EndDate <= endDate)
where (u.PersonId == person.Id)
select (int?)u.NumOfContributions).Sum() ?? 0;
然后更改您的条件:
var creditsSum = (from u in db.PERSON_SOCIAL_INSURANCE_CONTRIBUTIONS
where (u.StartDate > endDate || u.EndDate < startDate) &&
u.PersonId == person.Id
select (int?)u.NumOfContributions).Sum() ?? 0;
您不需要那么多的where语句,只需使用AND运算符即可。
您的Where
子句可以如下所示:
b.PERSON_SOCIAL_INSURANCE_CONTRIBUTIONS
.Where(
(c => c.StartDate > endDate || c.EndDate < startDate)
&& u.PersonId == person.Id
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.