开发者

Limit collection by enum using lambda

开发者 https://www.devze.com 2022-12-13 08:43 出处:网络
I have a collection of objects. One of the properties is \"Type\" which is an enu开发者_运维问答m. I want to limit the collection by \"type\" using a lambda and haven\'t quite figured out how to do it

I have a collection of objects. One of the properties is "Type" which is an enu开发者_运维问答m. I want to limit the collection by "type" using a lambda and haven't quite figured out how to do it.

Ideas?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消