How to attach a set of 'And' filters to a QueryOver query?
My method has a parameter which will determine which column to perform a where filter on, so to make things generic I need to be able to perform some logic on which column I want to perform the where on.
Is it possible to attach a .And clause to a given QueryOver<> query?
public List<..> GetABC(SomeType type)
{
NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "")
.开发者_Python百科And(x => x.a) // if type == SomeType.A then x.a, otherwise x.b (SomeType.B)
}
How could I do this?
I know when doing a criteria query I could create a criteria and then attach it to the query.
Sure.
var query = NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "");
if(type == SomeType.A)
{
query = query.And(x => x.a == ...);
}
else
{
query = query.And(x => x.b == ... );
}
The query will only be executed after the ".List()"
Update: I don't know if you're refering to something like this (in your comment)
var query = NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "");
Expression<Func<Blah, bool>> typePredicate = null;
if(type == SomeType.A)
{
typePredicate = x => x.a == ...;
}
else
{
typePredicate = x => x.b == ...;
}
query = query.Where(typePredicate);
Or probably you're more interested in something in the likes of Detached queries?
Not sure I completely understand the question ... but something like this?
public List<..> GetABC(SomeType type)
{
NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "")
.And(x => typeof(type) == SomeType.A ? x.a : x.b)
}
精彩评论