c# - 我想要第二个列表中的项目,其中包含第一个列表中的所有项目

标签 c# c#-4.0

我的代码是这样的

List<Entity> lists = CacheManager.GetAllEntity();
List<long> firstLists = lists .Select <Entity,long>(x=>x.ID).ToList<long>();
List<Entity2> secondLists = CacheManager.GetAllEntity2();

Entity2 的样子:

public class Entity2
{
    public long ID;     
    public long EntitytID;
}

现在假设 firstsLists 包含 {1,2,3,4}。 第二个包含

ID   EntitytID
1    1 
1    2
1    3
1    4
2    1
2    4
3    1
4    2
5    4

那么我的输出应该给我

ID   EntitytID
1    1 
1    2
1    3
1    4

因为项目 id 1 具有所有值 {1,2,3,4}

最佳答案

怎么样:

var results = secondLists
    .GroupBy(z => z.ID)
    .Where(z => firstLists.All(z2 => z.Select(z3 => z3.EntitytID).Contains(z2)))
    .SelectMany(z => z);

关于c# - 我想要第二个列表中的项目,其中包含第一个列表中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142298/

相关文章:

c# - Action<T> 等效于属性

c# - 使用 params ref T 参数创建方法

c++ - 多种类型的锯齿状数组

c# - 局部变量 'mydate' 在访问之前可能未初始化

c# - 打开前检查文件是否已被使用(网盘,C#)

c# - 是否可以执行以字符串表示的 C# 代码?

c# - 从 H2O 模型生成 C#

c# - 对 DropdownList 使用枚举 : Missing a using directive or assembly reference

c# - 有 .NET 的 Relaxer 吗? Relaxer 还活着吗? RelaxNG 可行吗?

c# - 在 C# 4.0 中,为什么方法中的输出参数不能协变?