2 回答
TA贡献1860条经验 获得超9个赞
您必须交替更改 LINQ 查询中的两个列表:
var objectsAdmited = allObjects.Where(b => typesAdmited.All(t => b.Types.Contains(t)));
TA贡献1875条经验 获得超5个赞
您可以使用 Linq 解决此问题。请参阅中间的小代码块 - 其余的是样板,使其成为最小完整的可验证答案:
using System;
using System.Collections.Generic;
using System.Linq;
public class ExampleObject
{
public int Id { get; set; }
public IEnumerable<int> Types { get; set; }
}
class Program
{
static void Main (string [] args)
{
var obs = new List<ExampleObject>
{
new ExampleObject
{
Id=1,
Types=new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21 }
},
new ExampleObject
{
Id=288,
Types=new List<int> { 94, 13, 11, 67, 256, 226, 82, 1, 66, 497, 21 }
},
};
var must_support = new List<int>{11, 67, 254, 256, 226, 82, }; // only Id 1 fits
var must_support2 = new List<int>{11, 67, 256, 226, 82, }; // both fit
// this is the actual check: see for all objects in obs
// if all values of must_support are in the Types - Listing
var supports = obs.Where(o => must_support.All(i => o.Types.Contains(i)));
var supports2 = obs.Where(o => must_support2.All(i => o.Types.Contains(i)));
Console.WriteLine ("new List<int>{11, 67, 254, 256, 226, 82, };");
foreach (var o in supports)
Console.WriteLine (o.Id);
Console.WriteLine ("new List<int>{11, 67, 256, 226, 82, };");
foreach (var o in supports2)
Console.WriteLine (o.Id);
Console.ReadLine ();
}
}
输出:
new List<int>{11, 67, 254, 256, 226, 82, };
1
new List<int>{11, 67, 256, 226, 82, };
1
288
- 2 回答
- 0 关注
- 192 浏览
添加回答
举报