Share via


How to DISTINCT->Check property IS Not NULL->Get First object in single LINQ query?

Question

Friday, July 18, 2014 3:31 PM

Hi All,

I have to DISTINCT->Check property IS Not NULL->Get First object in single LINQ query. I am using code below to do DISTINCT result but I also need to check if Address field is not Null or Empty. How to do that?

locations.GroupBy(x => x.ProductLine).Select(g => g.First()).OrderBy(o => o.ProductLine).ToList();

Oleg

All replies (1)

Friday, July 18, 2014 6:06 PM âś…Answered | 1 vote

Add a where clause after your Select:

.Where(x => !String.IsNullOrEmpty(x.Address))

If the IsNullOrEmpty doesn't work because of your provider then switch to the lengthier version:

x.Address != null && x.Address.Length > 0

Michael Taylor
http://msmvps.com/blogs/p3net