c# - 从范围 'x' 引用类型为 'myClass' 的错误变量 '',但未定义

标签 c# fluent-nhibernate restrictions

这段代码工作正常:

ICriteria criteria = GetSession().CreateCriteria<MyClass>();
criteria.Add(Restrictions.Where<MyClass>(x => x.Field1 >= myVariable));

但是下面的代码不起作用:

criteria.Add(Restrictions        
        .Where<MyClass>(x =>          
        (x.Field1 +
        x.Field2 +
        x.Field3 +
        x.Field4) >= myVariable));

上面的代码在执行时出现这个错误:

Variable 'x' of type 'myClass' referenced from scope '', but it is not defined

请帮忙(抱歉我的英语不好)。

萨拉

编辑 1

我的临时解决方案是:

var result = criteria.List<MyClass>();    
result.Where(x => (x.Field1 + x.Field2 + x.Field3 + x.Field4 >= myVariable));

和这项工作。我宁愿在选择之前放置 Where 子句...

编辑2

最终的解决方案是(根据@mhoff 的建议):

var result = criteria.List<MyClass>();    
result.Where(x => this.GetSum(x) >= myVariable);

... do something ...

... ToList()


private int GetSum(MyClass x) {
 return (x.Field1 + x.Field2 + x.Field3 + x.Field4);
}

最佳答案

如何将您的计算转换为属性(这也有封装 MyClass 细节的好处):

public class MyClass
{
    public int GetSum
    {
        get { return Field1 + Field2 + Field3 + Field4; }
    }
}

criteria.Add(Restrictions.Where<MyClass>(x => x.GetSum > myVariable));

关于c# - 从范围 'x' 引用类型为 'myClass' 的错误变量 '',但未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987454/

相关文章:

linq - NHIbernate (3.1) - Linq group by then order by count issue

c# - 保存实体时重复子条目

generics - Dart:有限制地扩展了通用类

c# - FTPClient 将文件从 Linux 移动到 Windows。知道他们什么时候结束发电

c# - 调试异步代码时如何关闭蓝色时钟显示?

c# - Azure 上的 GhostScriptSharp

c# - 它是 MSDN 中的错误代码吗?

c# - FluentNhibernate - HasMany 与复合键的关系

Hibernate Restrictions.in 与 Disjunction

facebook-apps - 应用程序的使用受到限制