我有一个对象列表,每个对象内部都有一个类型列表,类似于这样:
public class ExampleObject
{
public int Id {get; set;}
public IEnumerable <int> Types {get;set;}
}
例如:
var typesAdmited = new List<int> { 13, 11, 67, 226, 82, 1, 66 };
在对象列表中,我有一个像这样的对象:
Object.Id = 288;
Object.Types = new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21};
但是当我使用 linq 来获取所有承认类型的对象时,我会得到任何结果。我正在尝试这个:
var objectsAdmited = objects.Where(b => b.Types.All(t => typesAdmited.Contains(t)));
例子:
var typesAdmited = new List<int> { 13, 11, 67, 226, 82, 1, 66 };
var objectNotAdmited = new ExampleObeject {Id = 1, Types = new List<int> {13,11}};
var objectAdmited = new ExampleObject {Id = 288, Types = new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21}};
var allObjects = new List<ExampleObject> { objectNotAdmited, objectAdmited };
var objectsAdmited = allObjects.Where(b => b.Types.All(t => typesAdmited.Contains(t)));
我得到:
objectsAdmited = { }
它应该是:
objectsAdmited = { objectAdmited }
慕码人2483693
慕田峪4524236
相关分类