c# Linq - 获取包含整数列表的所有元素

我有一个对象列表,每个对象内部都有一个类型列表,类似于这样:


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 }


慕村225694
浏览 196回答 2
2回答

慕码人2483693

您必须交替更改 LINQ 查询中的两个列表:var&nbsp;objectsAdmited&nbsp;=&nbsp;allObjects.Where(b&nbsp;=>&nbsp;typesAdmited.All(t&nbsp;=>&nbsp;b.Types.Contains(t)));

慕田峪4524236

您可以使用 Linq 解决此问题。请参阅中间的小代码块 - 其余的是样板,使其成为最小完整的可验证答案:using System;using System.Collections.Generic;using System.Linq;&nbsp;public class ExampleObject{&nbsp; public int Id { get; set; }&nbsp; public IEnumerable<int> Types { get; set; }}class Program{&nbsp; static void Main (string [] args)&nbsp; {&nbsp; &nbsp; var obs = new List<ExampleObject>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; new ExampleObject&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Id=1,&nbsp; &nbsp; &nbsp; &nbsp; Types=new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21 }&nbsp;&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; new ExampleObject&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Id=288,&nbsp; &nbsp; &nbsp; &nbsp; Types=new List<int> { 94, 13, 11, 67,&nbsp; &nbsp; &nbsp; 256, 226, 82, 1, 66, 497, 21 }&nbsp;&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; };&nbsp; &nbsp; var must_support = new List<int>{11, 67, 254, 256, 226, 82, };&nbsp; // only Id 1 fits&nbsp; &nbsp; var must_support2 = new List<int>{11, 67, 256, 226, 82, };&nbsp; &nbsp; &nbsp; // both fit&nbsp; &nbsp; // this is the actual check: see for all objects in obs&nbsp;&nbsp; &nbsp; // if all values of must_support are in the Types - Listing&nbsp; &nbsp; var supports&nbsp; = obs.Where(o => must_support.All(i => o.Types.Contains(i)));&nbsp; &nbsp; var supports2 = obs.Where(o => must_support2.All(i => o.Types.Contains(i)));&nbsp; &nbsp; Console.WriteLine ("new List<int>{11, 67, 254, 256, 226, 82, };");&nbsp; &nbsp; foreach (var o in supports)&nbsp; &nbsp; &nbsp; Console.WriteLine (o.Id);&nbsp; &nbsp; Console.WriteLine ("new List<int>{11, 67, 256, 226, 82, };");&nbsp; &nbsp; foreach (var o in supports2)&nbsp; &nbsp; &nbsp; Console.WriteLine (o.Id);&nbsp; &nbsp; Console.ReadLine ();&nbsp; }}输出:new List<int>{11, 67, 254, 256, 226, 82, };1new List<int>{11, 67, 256, 226, 82, };1288
打开App,查看更多内容
随时随地看视频慕课网APP