cs0815
cs0815

Reputation: 17388

include null check to find index in list

Can I include a check for null in this code:

var index = someList.FindIndex(p => p.Bla1.Id == Dto.Id || p.Bla2.Id == Dto.Id);

Bla1 and Bla2 can both be null. Thanks.

Upvotes: 2

Views: 1309

Answers (2)

Sasha
Sasha

Reputation: 8850

Yes, you can =)

If you ask for how to do this:

var index = someList.FindIndex(p => (p.Bla1 != null && p.Bla1.Id == Dto.Id) || (p.Bla2 != null && p.Bla2.Id == Dto.Id));

But all depends on how you want to handle nulls

Upvotes: 0

Abe Miessler
Abe Miessler

Reputation: 85036

How about this:

var index = someList.FindIndex(p => (p.Bla1 != null && p.Bla1.Id == Dto.Id) 
                                 || (p.Bla2 != null && p.Bla2.Id == Dto.Id));

Upvotes: 11

Related Questions