Skip to main content
 首页 » 编程设计

C# Linq 数据结构,用于最终的数据绑定(bind)。最好的办法

2024年02月13日13bhlsheji

我有一套“零件”。每个部分都有一个名称和一组属性。属性是一组字符串键和值。例如:

Part 1 
    Width   200 
    Depth   400 
    Voltage 240V 

我将部件及其属性存储在这样的字典中:

Dictionary<string, Dictionary<string, string>> parts; 

所以我可以通过 parts.ContainsKey(part) 查看某个部分是否存在,如果存在,我就可以 查看 parts[part].ContainsKey(attribute) 中是否存在属性。到目前为止,还很平庸。

现在我要做的是数据绑定(bind)这个结构,或者生成一个集合 行,其中每一列都是一个属性。并非所有部分都具有所有属性,因此某些地方会有“空值”。我可以访问整个集合中找到的所有属性的列表,如List(实际生产系统中有59个可能的属性)。

我生成一组行的代码,假设第一列是零件名称,是 下面相当笨重的代码。它会产生一个字符串列表列表。一个字符串列表 对于每个部分(每个部分一个“行”)。

我很确定为此有一个简单的单行 Linq 语句。我希望我可以使用 它将数据绑定(bind)到列表或数据网格以在某个时候显示它。谁能帮我解决这个问题?

我在下面提供了一个完整的重现(添加一个新的 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(); 
            } 
        } 
    } 
} 

请您参考如下方法:

不存在简单的 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() 
                           )