简体   繁体   English

根据另一个列表中存在的属性过滤对象列表

[英]Filter a list of objects based on a property existing in another list

I've got a class named Container that includes an int property named Id .我有一个名为Container的 class ,其中包含一个名为Idint属性。

I've got a list of objects List<Container> named containers .我有一个名为containers的对象List<Container>

I've got a list of numbers List<int> named containerIds .我有一个名为containerIds的数字List<int>

How can I get a subset of containers where the Id is in containerIds ?如何获取IdcontainerIds中的容器子集?

Something like the following:类似于以下内容:

var result = containers.Where(x => x.Id IN containerIds);

You might use Contains method for that您可以为此使用Contains方法

var result = containers.Where(x => containerIds.Contains(x.Id));

Another option is to use Any Linq method另一种选择是使用Any Linq 方法

var result = containers.Where(x => containerIds.Any(i => i == x.Id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM