0
var boughtApples = apples.GroupBy(x => BoughtById);
var boughtCoconuts = coconuts.GroupBy(x => x.BoughtById);
var boughtOranges  = oranges.GroupBy(x => x.BoughtById);

我想获取购买所有三件商品的关键值,然后如果避风港购买BoughtById了全部三件,则将其删除。IGroupings

boughtApples   = [1,3,4,5]
boughtCoconuts = [1,2,4,9]
boughtOranges  = [6,3,4,10]

输出

boughtApples   = [4]
boughtCoconuts = [4]
boughtOranges  = [4]
4

2 回答 2

1

听起来像是Enumerable.Intersect()的工作:

int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };

IEnumerable<int> both = id1.Intersect(id2);

foreach (int id in both)
  Console.WriteLine(id);

/*
  This code produces the following output:

  26
  30
*/
于 2015-06-18T15:06:21.440 回答
1

要得到BoughtById每个你想要三组键的交集的那个:

var boughtAll = boughtApples.Select(gr => gr.Key)
  .Intersect(boughtCoconuts.Select(gr => gr.Key))
  .Intersect(boughtOranges.Select(gr => gr.Key));

现在,购买的所有将是一个IEnumerable<int>IQueryable<int>适当的。

然后获取要根据该交集过滤的相应组:

boughtApples = boughtApples.Where(grp => boughtAll.Contains(grp.Key));
boughtCoconuts = boughtCoconuts.Where(grp => boughtAll.Contains(grp.Key));
boughtOranges= boughtOranges.Where(grp => boughtAll.Contains(grp.Key));
于 2015-06-18T15:17:24.047 回答