以下是我的代码的样子
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;
}
现在假设第一个列表包含{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}
。
答案 0 :(得分:0)
怎么样:
var results = secondLists
.GroupBy(z => z.ID)
.Where(z => firstLists.All(z2 => z.Select(z3 => z3.EntitytID).Contains(z2)))
.SelectMany(z => z);
答案 1 :(得分:0)
var itemsGroupedById = SecondList.GroupBy(item => item.id, item => item).ToList();
var listToReturn = new List<Entity2>();
foreach(var group in itemsGroupedById)
{
var id = group.Key;
var entityIdsInThisGroup = group.Select(items => items.EntityId).ToList();
var intersection = entityIdsInThisGroup.Intersect(FirstList).ToList();
if(intersection.Count == FirstList.Count)
{
listToReturn.Add(group);
}
}
return listToReturn;
这将执行以下操作 -