Odys
Odys

Reputation: 9100

Do List.Exist using Linq

I want to check if any of the items in a list has a field set to true

at the moment I do this:

bool isPaid = visit.Referrals.Exists(delegate(AReferral r)
                                     {
                                         return r.IsPaidVisit;
                                     });

How can I do this using Linq might be trivial to some but can't figure if now.

Upvotes: 7

Views: 287

Answers (1)

Lasse Espeholt
Lasse Espeholt

Reputation: 17792

using System.Linq;

...

bool isPaid = visit.Referrals.Any(r => r.IsPaidVisit);

but why use the Linq library when you can do the following:

bool isPaid = visit.Referrals.Exists(r => r.IsPaidVisit);

Upvotes: 8

Related Questions