i trying implement query:
data.where(d => d.objectsa != null && d.objectsa.first().objectsb != null && d.objectsa.first().objectsb().first().nr == 1)
in nhibernate.linq, have error. when delete first() where() work. try solution don't need.
data.where(d => d.objectsa.where(a.objectsb.where(b=>b.nr == 1).any()).any());
can use firstordefault() or first() inside where() ?
edit : in db tables rows in example don't have null values.
first() throw exception if collection empty. using any() in where()
call should indeed solve problem, have write like:
data.where(d => d.objectsa != null && d.objectsa.any() && d.objectsa.first().objectsb != null && d.objectsa.first().objectsb.any() && d.objectsa.first().objectsb.first().nr == 1);
that's not nice, because first()
ends being called several times on same data. i'd suggest adding body lambda expression , using intermediate variables firstordefault():
data.where(d => { if (d.objectsa != null) { var firsta = d.objectsa.firstordefault(); if (firsta != null && firsta.objectsb != null) { var firstb = firsta.objectsb.firstordefault(); if (firstb != null) { return (firstb.nr == 1); } } } return false; });
edit: second code fragment above apparently not work linq nhibernate. if can use query syntax , don't have check if objectsa
, objectsb
null
, can write:
from d in data let firsta = d.objectsa.firstordefault() let firstb = (firsta != null ? firsta.objectsb.firstordefault() : null) (firstb != null && firstb.nr == 1) select d;
Comments
Post a Comment