我有一个包含多个类的列表,其中包含一个Integer(Id)属性.
我也有一个整数列表.
现在,我想将我的对象的List修剪为仅在整数列表中具有Property的那些类.
例:
List of MyObject [MyObjectA].Id = 1 [MyObjectB].Id = 2 [MyObjectC].Id = 3 [MyObjectD].Id = 4 List of Integer 1 2 Final list should be [MyObjectA] [MyObjectB]
我该怎么做?
你可以使用包含:
var finalList = originalList.Where(x => idList.Contains(x.Id)).ToList();
或加入:
var finalList = (from entry in originalList join id in idList on entry.Id equals id select entry).ToList();