1

I have a LINQ function similar to the one below:

public static Func<Contact, bool> activeContactsFilter = c =>
    !c.suspended &&
    !c.expired &&
    (c.status_id == 1 || c.status_id == 7);

This works great when I need to find out which Contacts amongst an IEnumerable of contacts are "active":

var activeContacts = allContacts.Where(activeContactsFilter);

But what if I want to just check if ONE specific contact is "active"? How do I use this same function for a test on a single object?

Jimbo
  • 22,379
  • 42
  • 117
  • 159

1 Answers1

6

It's really simple - just call the Func like it was, well, a function:

bool contactIsActive = activeContactsFilter(contactToCheck);

Funcs aren't specifically LINQ-related, they're just useful for LINQ. You can store a method in a Func of the correct type, and (as above) you can call a Func like a method.

Take a look here for more information on Funcs.

Community
  • 1
  • 1
Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Thanks for the quick and detailed answer (also @BartoszKP) - will accept as soon as I'm allowed to by SO. – Jimbo Aug 21 '14 at 09:18