I want to do a simple lambda expression like this:
IList<MyEntity1> list = GetSomeList();
MyEntity1 result = list.SingleOrDefault<MyEntityList>(
e => GetMyEntity2(e) != null && GetMyEntity2(e).Id != null && GetMyEntity2(e).Id > 0
);
That works perfectly, but getting MyEntity2 from MyEntity1 is not so simple so I would like to declare a variable into the lambda expression to save MyEntity2 and use it, instead of calling again and again to GetMyEntity2 method. Is that possible?
Note: The code is just an example that reflects my real case.
Thanks!
Well, first off, are you trying to use this in linq to sql / entity framework / other?
If not, then just do this
If you want to use the "query comprehension" syntactic form you can do this:
Note also that the middle "where" clause might not be necessary. If "entity2.Id" is a nullable int then it will be correctly checked for null by the lifted
>
operator.You can use the
Select
operator:Or, since you're not even using the
Item
after pushing it throughGetMyEntity2
you could just have: