Bill Martin
Bill Martin

Reputation: 4943

Limit collection by enum using lambda

I have a collection of objects. One of the properties is "Type" which is an enum. I want to limit the collection by "type" using a lambda and haven't quite figured out how to do it.

Ideas?

Upvotes: 0

Views: 1809

Answers (2)

Keith
Keith

Reputation: 155662

You could also use Linq syntax:

var filtered = 
    from p in items
    where p.Type == MyEnum.ValueIWant
    select p;

This will compile to exactly the same code as @Jason's suggestion.

Upvotes: 2

jason
jason

Reputation: 241621

MyEnum type = MyEnum.ValueIWant;
var filtered = items.Where(p => p.Type == type);

Upvotes: 11

Related Questions