I having some problem in Optional condition in Lambda express like
var shouldCheckDate = !string.IsNullOrEmpty(fromDate);
var result = (from r in db.Notify
where r.ApplicationId == applicationId
&& (shouldCheckDate || r.CreatedDate.Date > date)
select r
).Count;
problem is here it always excuting both condition either shouldCheckDate
true are false. I am doing any thing wrong?
这是由于以下事实:与C#相比,LINQ被转换为SQL,并且SQL执行条件的所有部分,无论它们中的任何一个是否都已返回true。
You could compose the IQueryable by adding the where clauses only when needed:
var query = db.Notify;
query = query.Where(r => r.ApplicationId == applicationId);
if (shouldCheckDate) {
query = query.Where(r => r.CreatedDate.Date > date);
}
var result = query.Count();
You have OR statement here,
(shouldCheckDate || r.CreatedDate.Date > date)
so even one of these conditions which is true
is enough for whole statement to be true.
Maybe r.CreatedDate.Date
is always > date
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.