我有一个Linq查询,看起来像这样:
var query = from x in table where SomeFunctionReturnsBool() select;
private bool SomeFunctionReturnsBool()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
这返回和异常说"SomeFunctionReturnsBool没有支持的SQL转换".我知道这是因为它想将"SomeFunctionReturnsBool"视为一个表达式来评估为SQL,但它不能.
虽然这个Linq查询并不复杂,但真正的问题是.我怎样才能完成我在这里尝试做的事情,即打破查询的各个部分,希望它更具可读性?
杰夫
更新 好的答案.我现在正在尝试使用表达式,但是这段代码让我"无法解析方法Where(lambda表达式)":
var query = from x in table where SomeFunctionReturnsBool() select x;
private Expression<Func<EligibilityTempTable, bool>> SomeFunctionReturnsBool
{
return (x) => true;
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用Expression<Func<YourType, bool>>谓词...
var query = from x in table where SomeFunctionReturnsBool() select;
Run Code Online (Sandbox Code Playgroud)
编辑:我通常不会按照上面显示的方式进行操作......我只是从上面的代码中获取.这是我通常实现它的方式.因为那时你可以使用其他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;
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用PredicateBuilder,它允许你构建一个表达式,用于你的Where ...
| 归档时间: |
|
| 查看次数: |
4589 次 |
| 最近记录: |