扩展我的 linq 查询时遇到一些困难。
假设我有以下数据:
Vault Items
----- -----
1 100, 102
2 100, 102
3 101
4 101
我想展平这个数组,所以我得到了这个:
Items Vaults
----- ------
100, 102 1, 2
101 3, 4
我目前拥有的代码是这个
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
var Vaults = new[]
{
new Vault
{
Id = 1,
Contents = new Contents
{
Items = new[]
{
new Item
{
Id = 100,
Number = "100"
},
new Item
{
Id = 102,
Number = "102"
}
}
}
},
new Vault
{
Id = 2,
Contents = new Contents
{
Items = new[]
{
new Item
{
Id = 100,
Number = "100"
},
new Item
{
Id = 102,
Number = "102"
}
}
}
},
new Vault
{
Id = 3,
Contents = new Contents
{
Items = new[]
{
new Item
{
Id = 101,
Number = "101"
}
}
}
},
new Vault
{
Id = 4,
Contents = new Contents
{
Items = new[]
{
new Item
{
Id = 101,
Number = "101"
}
}
}
}
};
var grouping = Vaults.SelectMany(
c => c.Contents.Items.Select(s => new { Item = s, Vault = c }))
.GroupBy(o => o.Item.Id)
.Select(
grouping1 =>
new Tuple<Item, IEnumerable<Vault>>(
grouping1.First().Item, grouping1.Select(o => o.Vault)));
foreach (var tuple in grouping)
{
Console.WriteLine("Item(s) {0}: Vault {1}", tuple.Item1.Number,
String.Join(", ", tuple.Item2.Select(c => c.Id.ToString()).ToArray()));
}
}
}
public class Vault
{
public long Id { get; set; }
public Contents Contents { get; set; }
}
public class Contents
{
public IEnumerable<Item> Items { get; set; }
}
public class Item
{
public long Id { get; set; }
public string Number { get; set; }
}
这个查询还不能很好地工作,因为它没有在项目列表上分组,结果类似于:
Item Vaults //Note: this isn't the output I need
---- ------
100 1, 2
102 1, 2
101 3, 4
谁能指出我修复此查询的正确方向?对于这个例子,我们可以假设 Vault 共享的项目总是相同的。(意味着 Vault 2必须包含与 Vault 1 完全相同的物品)