Reputation: 2908
I try to re-factor query 1 to query 2 syntax... because of more more readable etc. paramaters like "group, begindate, enddate and excludeManifestatie are passed via argument (method parms).. I check whether they are blank or not )passed or not so I can built my dynamic Linq SQL.
But I get error in Query 1 when I use ....HasValue check. It says: "Cannot implicitly convert type 'System.Linq.IQueryable' to 'bool'"
Query 2 works fine and that's what I want, but it has complicated syntax, so I need to correct Query 1 syntax.
QUERY 1:
((String.IsNullOrEmpty(group)) ? (a.id != -1) : (a.no_group == group))
&& ((group.Equals("990") || (group == string.Empty)) ? (!a.displaylocal.HasValue || a.displaylocal.Value == false) : (a.id != -1))
&& ((a.begindate.HasValue ? DateTime.Now >= a.begindate.Value : a.id != -1) &&
(a.enddate.HasValue ? DateTime.Now <= a.enddate.Value : a.id != -1))
&& ((String.IsNullOrEmpty(excludeManifestatie)) ? (a.id != -1) : (a.type_manifestation != excludeManifestatie))
NEW/BETTER WAY to WRITE:
QUERY 2:
var query = dc.agenda.AsQueryable();
if (!string.IsNullOrEmpty(group))
query = query.Where(a => a.no_group == group);
if (group.Equals("990") || group.Equals(string.Empty))
if ((query.Where(a => a.displaylocal.HasValue))) //Gives ERROR
query = query.Where(a => a.displaylocal >= DateTime.Now);
if (!string.IsNullOrEmpty(excludeManifestatie))
query = query.Where(a => a.type_manifestation != excludeManifestatie);
if (query.Where(a => a.begindate.HasValue)) //Gives ERROR
query = query.Where(a => a.begindate <= DateTime.Now);
if ((query.Where(a => a.enddate.HasValue))) //Gives ERROR
query = query.Where(a => a.enddate >= DateTime.Now);
Upvotes: 0
Views: 2788
Reputation: 174369
You can't do it like this. If you want to include a condition based on data in each individual row, you need to resort to your way. The method I showed you in your last question only works if the condition should be added based on a external parameter. I would use this condition:
query = query.Where(a => (a.begindate.HasValue && a.begindate <= DateTime.Now) ||
!a.begindate.HasValue);
Your complete code looks like this:
var query = dc.agenda.AsQueryable();
if (!string.IsNullOrEmpty(group))
query = query.Where(a => a.no_group == group);
if (group.Equals("990") || group.Equals(string.Empty))
query = query.Where(a => (a.displaylocal.HasValue && a.displaylocal >= DateTime.Now) ||
!a.displaylocal.HasValue);
if (!string.IsNullOrEmpty(excludeManifestatie))
query = query.Where(a => a.type_manifestation != excludeManifestatie);
query = query.Where(a => (a.begindate.HasValue && a.begindate <= DateTime.Now) ||
!a.begindate.HasValue);
query = query.Where(a => (a.enddate.HasValue && a.enddate >= DateTime.Now) ||
!a.enddate.HasValue);
Upvotes: 4
Reputation: 7614
It's not when you use HasValue
, it's when you use Where
as a condition.
If you want to check if there are any elements that conform to the HasValue
condition, you should use Any
instead of Where
:
if (query.Any(a => a.begindate.HasValue))
Upvotes: 1
Reputation: 17784
try changing from
query = query.Where(a => a.no_group == group);
to
query = query.SingleOrDefault(a => a.no_group == group);
Upvotes: -2