c# - 从另一个列表中过滤 IEnumerable
问题描述
我有一个IEnumerable<int>
with items1, 5, 10, 85, 96
和另一个
IEnumerable<Hw> lstHw
,Hw
class 有 property HwID
。
我想过滤以获取( )IEnumerable<int>
中不存在的值。IEnumerable<Hw>
HwID
所以输出应该给我:1, 5, 85
我们怎么能做到这一点?
解决方案
如果我正确地阅读此内容,您希望从 中获取所有整数,这些整数在另一个列表中List<int>
没有对应的匹配项。Hw
HWId
如果是这样,您可以使用这些System.Linq
方法Where
过滤掉其中All
的项目lstHw
没有HwID
匹配的项目:
var ints = new List<int> {1, 5, 10, 85, 96};
var lstHw = new List<Hw> {new Hw {HwID = 10}, new Hw {HwID = 96}};
var results = ints.Where(i => lstHw.All(hw => hw.HwID != i));
更新
根据您在评论部分的代码,您似乎实际上有两个List<int>
集合(嗯,一个是 a List<uint>
)。这是你的评论:
IEnumerable<Int32> hwids = scopedZcat.GetProductsByFamily(auto.PlatformID);
IEnumerable<uint> selectedHwId = lstZcatCases
.Where(c => c.CaseID != auto.CaseID)
.Select(i => i.HWID)
.ToList();
// Now I want the hwids which are not there in selectedHwid
如果是这种情况,那么这应该可以解决问题:
// Note that we have to cast a `uint` to an `int`
var results = hwids.Except(selectedHwId.Select(id => (int)id));
推荐阅读
- python - 为什么这个测试方法只能在单独的类中运行?
- conditional-statements - 在 Netlogo 的条件语句中,括号是否与“and”和“or”一起使用?
- graphql - graphql查询中的多项选择
- numpy - 在 google colaboratory 上处理自动编码器时如何处理大型训练集?
- linux - 如何使用 grep 提取第三列?
- javascript - 如何在 HTML/CSS 中有可选的分屏布局?
- node.js - 如何处理多个 API 请求
- apache - .htaccess 重定向除某个域的一个和另一个内部重定向之外的所有页面
- python-3.x - 如何在 django-rest 中设置默认的内部序列化程序数据?
- python - 如何在 Python 的 sys.stdin 中的一行中提取多个单词?