我有一套“部件”。每个部分都有一个名称和一组属性。属性是一组字符串键和值。例如:
Part 1
Width 200
Depth 400
Voltage 240V
我将这些部件及其属性存储在这样的字典中:
Dictionary<string, Dictionary<string, string>> parts;
所以我可以看到一个部件是否存在parts.ContainsKey(部分),如果存在,我可以 看看零件[part]是否存在属性.ContainsKey(属性)。到目前为止,如此平庸。
现在我要做的是数据绑定此结构,还是生成一个集合 行,每列是属性。并非所有部分都具有所有属性,因此在某些地方会有“空值”。我可以访问整个集合中所有属性的列表,如List(实际生产系统中有59个可能的属性)。
我生成一组行的代码(假设第一列是部件名称)是 下面相当笨重的代码。它产生一个字符串列表列表。一个字符串列表 每个部分(每个部分一个“行”)。
我很确定这是一个简单的单线Linq声明。我希望我能用 它将数据绑定到列表或数据网格以在某个时刻显示它。任何人都可以帮我解决这个问题吗?
我在下面提供了一个完整的repro(添加一个新的C#控制台项目),以及一些示例数据。
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// The set of parts.
Dictionary<string, Dictionary<string, string>> hashPartToAttributes = new Dictionary<string,Dictionary<string,string>>();
hashPartToAttributes.Add("Part 1", new Dictionary<string,string>());
hashPartToAttributes.Add("Part 2", new Dictionary<string,string>());
hashPartToAttributes.Add("Part 3", new Dictionary<string,string>());
hashPartToAttributes.Add("Part 4", new Dictionary<string,string>());
// Add in all attributes for all of the parts.
{
hashPartToAttributes["Part 1"].Add("Width", "200");
hashPartToAttributes["Part 1"].Add("Height", "400");
hashPartToAttributes["Part 1"].Add("Depth", "600");
hashPartToAttributes["Part 2"].Add("Width", "300");
hashPartToAttributes["Part 2"].Add("Height", "700");
hashPartToAttributes["Part 2"].Add("Depth", "100");
hashPartToAttributes["Part 2"].Add("Voltage", "240V");
hashPartToAttributes["Part 3"].Add("Voltage", "110V");
hashPartToAttributes["Part 3"].Add("Bandwidth", "25");
hashPartToAttributes["Part 3"].Add("Frequency", "5");
hashPartToAttributes["Part 3"].Add("Height", "900");
hashPartToAttributes["Part 4"].Add("Width", "150");
hashPartToAttributes["Part 4"].Add("Height", "740");
hashPartToAttributes["Part 4"].Add("Depth", "920");
hashPartToAttributes["Part 4"].Add("Voltage", "240V");
hashPartToAttributes["Part 4"].Add("Bandwidth", "40");
hashPartToAttributes["Part 4"].Add("Frequency", "5");
}
// The complete set of all attributes (column headings)
List<string> attributeKeys = new List<string>() {
"Width", "Height", "Depth", "Voltage", "Bandwidth", "Frequency"
};
// Now construct a row for each part.
List<List<string>> rows = new List<List<string>>();
foreach (string part in hashPartToAttributes.Keys)
{
List<string> row = new List<string>() { part };
foreach (string key in attributeKeys)
{
Dictionary<string, string> attributes = hashPartToAttributes[part];
if (attributes != null && attributes.ContainsKey(key))
{
row.Add(attributes[key]);
}
else
{
row.Add(null);
}
}
rows.Add(row);
}
// Print out headings.
Console.Write("{0, -10}", "Part");
foreach (string heading in attributeKeys)
{
Console.Write("{0, -10}", heading);
}
Console.WriteLine();
Console.WriteLine();
// Print out the rows
foreach (List<string> row in rows)
{
foreach (string item in row)
{
if (item != null)
{
Console.Write("{0, -10}", item);
}
else
{
Console.Write("{0, -10}", "-");
}
}
Console.WriteLine();
}
}
}
}
答案 0 :(得分:4)
不存在简单的LINQ语句,但您可以使用以下语句:
hashPartToAttributes.Select(
p => new [] { p.Key }.Concat(
attributeKeys.GroupJoin(p.Value, ak => ak, a => a.Key,
(ak, a) => a.Select(x => x.Value).SingleOrDefault())
.DefaultIfEmpty())
.ToArray()
)