LINQ方法语法 - 无法转换为bool[英] Linq Method Syntax - Can not convert IQueryable to Bool

本文是小编为大家收集整理的关于LINQ方法语法 - 无法转换为bool的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我尝试将查询1重新查询1查询2语法...因为更多可读性等. 诸如" groun,begindate,enddate和excludemanifestatie"之类的参数通过参数(方法parms).我检查它们是否空白),以便我可以构建我的动态linq sql.

.

但是,当我使用.... hasValue检查时,我会在查询1中遇到错误.它说: "不能隐式将类型'System.Linq.iqueryable'转换为'bool'"

查询2正常工作,这就是我想要的,但是它具有复杂的语法,因此我需要更正查询1语法.

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

新/更好的写作方法:

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

推荐答案

您不能这样做.如果要根据每个单个行中的数据包括一个条件,则需要诉诸方法.我在最后一个问题中向您展示的方法只有在基于外部参数添加条件的情况下才起作用.我会使用这种情况:

query = query.Where(a => (a.begindate.HasValue && a.begindate <= DateTime.Now) || 
                         !a.begindate.HasValue);

您的完整代码看起来像这样:

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

其他推荐答案

不是当您使用HasValue时,是当您使用Where作为条件时.

如果要检查是否有符合HasValue条件的任何 元素,则应使用Any而不是Where:

if (query.Any(a => a.begindate.HasValue))

其他推荐答案

尝试从

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

to

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

本文地址:https://www.itbaoku.cn/post/1557158.html

问题描述

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

推荐答案

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

其他推荐答案

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

其他推荐答案

try changing from

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

to

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