Linq Method Syntax - Can not convert IQueryable to Bool

随声附和 提交于 2019-12-12 05:36:18

问题


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);

回答1:


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);



回答2:


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))



回答3:


try changing from

query = query.Where(a => a.no_group == group);

to

query = query.SingleOrDefault(a => a.no_group == group);


来源:https://stackoverflow.com/questions/5857261/linq-method-syntax-can-not-convert-iqueryable-to-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!