简体   繁体   English

LINQ和Nhibernate:使用模型属性创建表达式

[英]LINQ and Nhibernate: create an Expression using a model property

I am getting started with LINQ and NHibernate, can you help me get oriented please: 我正在开始使用LINQ和NHibernate,请您能帮助我确定方向:

I need to pass a lambda statement to nhibernate .QueryOver() which is conditional based on a property on my model: 我需要将lambda语句传递给nhibernate .QueryOver(),该条件是基于我模型的属性的条件:

if (model.PropertyA != String.Empty) {
    var searchResults = nhibSession.QueryOver<type>(x => 
                                             x.propA == model.PropertyA)
                                   .List();
}

Is there a better way to do this using a C# Expression instead of a lambda statement? 有没有更好的方法使用C#表达式而不是lambda语句来做到这一点? How do I create an Expression using model.PropertyA? 如何使用model.PropertyA创建表达式? Do I use Expression.Property() or Expression.Field()? 我是否使用Expression.Property()或Expression.Field()?

thanks 谢谢

How do I create an Expression using model.PropertyA? 如何使用model.PropertyA创建表达式?

I suspect you should be using Expression.Constant - even though it doesn't "feel" like a constant in the normal sense, it's constant for that expression as the model isn't part of the input to the expression. 我怀疑您应该使用Expression.Constant尽管它在正常情况下不像常量那样“感觉”到它,但对于该表达式来说它是常量因为模型不是表达式输入的一部分。

Expression foo = Expression.Constant(model.PropertyA);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM