Reputation: 4276
I have this code
var x = xId.HasValue ? _xRepository.Get(xId) : null;
var result = _yRepository.Find()
.Where(y=> x == null || x.Ys.Contains(y)).ToArray();
NHibernate breaks on this line .Where(y=> x == null || x.Ys.Contains(y))
where it executes all conditions and it does not stop on the first condition if the value of x is equal to null.
could any one help me to solve this issue.
Upvotes: 1
Views: 29
Reputation: 123861
In this case, we can evaluate the x
before the SQL is created e.g:
var toFilter = _yRepository.Find();
// should we handle x at all?
if (x != null)
{
toFilter = toFilter.Where(y => x.ys.Contains(y));
}
var emailAddresses = toFilter.ToArray();
Extend
with an extension like this
public static class MyExtensions
{
public static IQueryable<T> MyContains<T, TFilter>(
this IQueryable<T> list,
TFilter x,
Expression<Func<T, bool>> filterFunc)
{
if (x == null)
{
return list;
}
return list.Where(filterFunc);
}
}
we can make such filtering at one line
var emailAddresses = _yRepository
.Find()
.MyContains(x, y => x.ys.Contains(y))
.ToArray();
Upvotes: 1