Where子句不与parantheses一起工作 [英] Where clause not working with parantheses

查看:176
本文介绍了Where子句不与parantheses一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设使用NH 3.4和RepositoryPattern的以下查询

  var list = _repository 
.QueryOver()
.Where(x =>(x.Age> 20))//注意到parantheses
.Future()
.ToList();

如果添加了这些缺陷,NH将无法正常工作,并导致SO异常。如果用<$ c替换 .Where(x =>(x.Age> 20)),那么

$ c> .Where(x => x.Age> 20)
按预期方式运作

任何线索为什么它不能与额外的偏食一起工作?



注意

图片。在生产中,我通过一个参数 Expression< Func< Person,bool>>来传递 .Where(...)

解决方案

我怀疑错误在那里:

 表达式< Func< MyClass,bool>> mc1 = x => (x.ID> 20); 
Expression< Func< MyClass,bool>> mc2 = x => x.ID> 20;

var body1 = mc1.Body.NodeType; // GreatThan
var body2 = mc2.Body.NodeType; // GreatThan

括号被编译器删除。表达式树语言(类系统)中没有任何内容显式地表示括号。

Suppose the following Query using a NH 3.4 and RepositoryPattern

var list = _repository
     .QueryOver()
     .Where(x => (x.Age > 20))  // notice the parantheses
     .Future()
     .ToList();

Whith these parantheses added the NH is failing to work, and causes a SO exception.

If replacing .Where(x => (x.Age > 20)) with .Where(x => x.Age > 20) it works as expected.

Any clues on why it doesn't work with extra parantheses?

Note

This is a simplified scenario from the bigger picture. In production i'm passing that .Where(...) through a parameter Expression<Func<Person, bool>> where

解决方案

I have a doubt the error is there:

Expression<Func<MyClass, bool>> mc1 = x => (x.ID > 20);
Expression<Func<MyClass, bool>> mc2 = x => x.ID > 20;

var body1 = mc1.Body.NodeType; // GreatThan
var body2 = mc2.Body.NodeType; // GreatThan

The brackets are removed by the compiler. There is nothing in the Expression tree "language" (class system) to explicitly represent a bracket.

这篇关于Where子句不与parantheses一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆