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

BoughtById3 つのアイテムすべてを購入したキー値を取得し、3 つすべてを購入した場合はすべてから削除したいと考えています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

それぞれにあるを取得するBoughtByIdには、3 つのキー セットの交差が必要です。

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

buyAll は必要に応じてIEnumerable<int>orIQueryable<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 に答える
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 に答える