Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to check for Null in Lambda Expressions

Tags:

c#

lambda

I have seen a lot of questions on this but was not able to find a clean solution:

I have the following lambda expression:

var result = Store.FirstOrDefault(x.Products.Coupon[0] == 100);

I would like to check for null for the Coupon collection to check to see if its not null and then compare the first coupon with the value 100. What would be a clean way to check for NULL for Coupon in the lambda? I do not want to use an extension method to check for null. I would like to do the check inline.

like image 304
user1527762 Avatar asked Mar 10 '13 21:03

user1527762


1 Answers

var result = Store.FirstOrDefault(x => x.Products.Coupon != null && x.Products.Coupon.Any() && x.Products.Coupon[0] == 100);
like image 136
Corey Sunwold Avatar answered Nov 11 '22 19:11

Corey Sunwold