[英]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.