加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

linq-to-sql – Linq:简单布尔函数返回linq异常

发布时间:2020-12-12 06:28:32 所属栏目:MsSql教程 来源:网络整理
导读:我有一个看起来像这样的 Linq查询: var query = from x in table where SomeFunctionReturnsBool() select;private bool SomeFunctionReturnsBool(){ return true;} 这返回和异常说“SomeFunctionReturnsBool没有支持的SQL转换”.我知道这是因为它想将“Some
我有一个看起来像这样的 Linq查询:
var query = from x in table where SomeFunctionReturnsBool() select;

private bool SomeFunctionReturnsBool()
{
    return true;
}

这返回和异常说“SomeFunctionReturnsBool没有支持的SQL转换”.我知道这是因为它想将“SomeFunctionReturnsBool”视为一个表达式来评估为SQL,但它不能.

虽然这个Linq查询并不复杂,但实际的查询并不复杂.我怎样才能完成我在这里尝试做的事情,即打破查询的各个部分,希望能让它更具可读性?

杰夫

UPDATE
好的答案.我现在正在尝试使用表达式,但是这段代码让我“无法解析方法Where(lambda表达式)”:

var query = from x in table where SomeFunctionReturnsBool() select x;

private Expression<Func<EligibilityTempTable,bool>> SomeFunctionReturnsBool
{
  return (x) => true;
}

解决方法

另一种方法是使用Expression< Func< YourType,bool>>谓语…
var query = from x in table where SomeFunctionReturnsBool() select;

编辑:我通常不会按照上面显示的方式进行操作…我只是从上面的代码中获取它.这是我通常实现它的方式.因为那时您可以使用其他Enumerable方法或在调试期间注释掉它们.

var results = table.Where(SomeFunctionReturnsBool())
    .OrderBy(yt => yt.YourProperty)
    //.Skip(pageCount * pageSize) //Just showing how you can easily comment out parts...
    //.Take(pageSize)
    .ToList(); //Finally executes the query...

private Expression<Func<YourType,boo>> SomeFunctionReturnsBool()
{
    return (YourType yt) => yt.YourProperty.StartsWith("a")
        && yt.YourOtherProperty == true;
}

我更喜欢使用PredicateBuilder,它允许你构建一个表达式,用于你的Where …

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读