从另一个列表中过滤 IEnumerable
Filter an IEnumerable from another list
我有一个 IEnumerable<int>
和 1, 5, 10, 85, 96
个项目
IEnumerable<Hw> lstHw
,其中 Hw
class 有 属性 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>
集合(好吧,一个是 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));
我有一个 IEnumerable<int>
和 1, 5, 10, 85, 96
个项目
IEnumerable<Hw> lstHw
,其中 Hw
class 有 属性 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>
集合(好吧,一个是 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));