16

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!

3 Answers 3

38

Well, first off, are you trying to use this in linq to sql / entity framework / other?

If not, then just do this

list.SingleOrDefault(e => {
   var entity = GetMyEntity2(e);

   return entity != null && entity.Id != null && entity.Id > 0;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that just what I was looking for, it works great. Thanks!
9

If you want to use the "query comprehension" syntactic form you can do this:

var query = from entity1 in list
            let entity2 = GetMyEntity2(entity1)
            where entity2 != null
            where entity2.Id != null 
            where entity2.Id > 0
            select entity1;
var result = query.SingleOrDefault();

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.

7 Comments

@Eric Lippert didn't knew this syntactic. But I think I see clearer and easier to understand @Darren Kopp solution. Thanks anyway! About the middle "where" clause, as I said, this was just an example, those aren't the real clauses. But again thanks anyway, didn't knew I could compare int?.
@Eric - Is there a reason you use multiple wheres instead of a single where with &&?
@Steven: Just because it looks nice. Incidentally, Jon Skeet has a blog article today about the performance implications of this choice. They are a bit surprising.
Wouldn't it be better to use && then nested where clauses? You can format them to look pretty much the same, they're more expressive ("where x and y" is more expressive than "where x where y") and they result in simpler-to-run code.
@configurator: I don't see one way as particularly better or worse than the other.
|
6

You can use the Select operator:

IList<MyEntity1> list = GetSomeList();

MyEntity1 result = list
    .Select(x => new { Item = x, Entity2 = GetMyEntity2(x) })    
    .SingleOrDefault(x => x.Entity2 != null && x.Entity2.Id != null && x.Entity2.Id > 0);

Or, since you're not even using the Item after pushing it through GetMyEntity2 you could just have:

MyEntity1 result = list
    .Select(x => GetMyEntity2(x))    
    .SingleOrDefault(x => x != null && x.Id != null && x.Id > 0);

3 Comments

Thanks @Kirk Woll, but I found easer @Darren Kopp solution. Does this one have some benefit over the other one?
@Diego, in your example, not so much. However, my solution is more general and will work in Linq-To-Sql and such as well. (at least, it would so long as you're not calling custom methods like GetMyEntity2). It also has the vague benefit of being "purely functional".
Ok, +1 because I'll have it mind for other cases where the @Darren Kopp solution does not apply, thanks! However for this case I think the answer I was looking for (and simple solution) is @Darren's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.