简体   繁体   中英

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:

I need to pass a lambda statement to nhibernate .QueryOver() which is conditional based on a property on my model:

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? How do I create an Expression using model.PropertyA? Do I use Expression.Property() or Expression.Field()?

thanks

How do I create an Expression using 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 foo = Expression.Constant(model.PropertyA);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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