【问题标题】:Fill Data To a particular Cell In DataTable -C#将数据填充到 DataTable -C# 中的特定单元格
【发布时间】:2018-08-31 23:06:36
【问题描述】:

我有类似的数据

 ConcurrentDictionary<string, ConcurrentDictionary<string,string> OneTwoThree =
                new ConcurrentDictionary<string, ConcurrentDictionary<string, string>();

我想要这样的结果Final Image 我尝试了什么:

 DataTable dt = new DataTable();
 dt.Columns.Add("TwoDetails");
 ISet<string> two = new HashSet<string>();
 Parallel.ForEach(OneTwoThree , One=>
             {
                 dt.Columns.Add(One.Key);
                 foreach(var Two in One.Value)
                 { 
                  two.Add(Two.Key); // To get Distinct Values
                 }
             });
 foreach(var item in two)
 { 
   var row = dt.NewRow();
   row["TwoDetails"] = row;
 }

现在我没有将“三值”附加到特定单元格的想法,如图所示。

任何建议。

【问题讨论】:

  • @TimSchmelter.. 如果行名和列名存在,你如何在单元格中插入数据(这是行名和列名的交集)。
  • @sachin .. 我在这里处理行名,在数据表中创建新行时对其进行排序。
  • 有什么建议吗?

标签: c# dictionary datatable


【解决方案1】:

您不能使用名称访问数据表行,但使用行号可以在数据表中找到特定行。因此,您需要创建一个类

 public class DataTableDetails
{
    public string RowName { get; set; }
    public int RowNumber { get; set; }
}

这不是有效的解决方案,但可以帮助你

    ISet<string> two = new HashSet<string>();
    List<DataTableDetails> dtdetaills=new List<DataTableDetails>();
     Parallel.ForEach(OneTwoThree , One=>
                 {
                     dt.Columns.Add(One.Key);
                     foreach(var Two in One.Value)
                     { 
                      two.Add(Two.Key); // To get Distinct Values
                     }
                 });
int count=0;
foreach(var item in two)
 { 
   var row = dt.NewRow();
   row["TwoDetails"] = row;
   DataTableDetails details = new DataTableDetails();
   details.RowName = item;
   details.RowNumber = count++; // we can easily get row number 
   dtdetails.Add(details);
 }

最后

foreach(var One in OnrTwoThree)
            {
                foreach(var Two in One.Value)
                {
                   foreach(var rowdetails in dtdetails)
                    {
                        if(Two.Key==rowdetails.RowName)
                        {
                            dt.Rows[rowdetails.RowNumber][One.Key] = Two.Value;
                        }

                    }
                }
            }

【讨论】:

    【解决方案2】:

    另一个数据透视表问题。完成了 1000 次。请参见下面的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    
    namespace ConsoleApplication31
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, Dictionary<string, string>> OneTwoThree = new Dictionary<string, Dictionary<string, string>>() {
                    {"A", new Dictionary<string,string>(){{"U","s"}, {"Z","a"}}},
                    {"B", new Dictionary<string,string>(){{"W","e"},{"X","d"},{"Y","d"}}},
                    {"C", new Dictionary<string,string>(){{"V","f"}, {"W","a"},{"Z","w"}}},
                };
    
                string[] columns = OneTwoThree.Select(x => x.Key).OrderBy(x => x).ToArray();
    
                DataTable dt = new DataTable();
                dt.Columns.Add("TwoDetails", typeof(string));
                foreach(string column in columns)
                {
                    dt.Columns.Add(column, typeof(string));
                }
    
                string[] rows = OneTwoThree.Select(x => x.Value.Select(y => y.Key)).SelectMany(x => x).Distinct().OrderBy(x => x).ToArray();
                var flip = rows.Select(x => new { row = x, columns = OneTwoThree.Where(y => y.Value.ContainsKey(x)).Select(y => new { col = y.Key, value = y.Value[x] }).ToList() }).ToList();
                //create pivot table
                foreach (var row in flip)
                {
                    DataRow newRow = dt.Rows.Add();
                    newRow["TwoDetails"] = row.row;
                    foreach (var column in row.columns)
                    {
                        newRow[column.col] = column.value;
                    }
                }
            }
    
        }
    }
    

    【讨论】:

    • 不错的一个 .ty。但是在您的 OneTwoThree 字典中应该只包含 3 个键(即 A、B、C),而内部字典应该包含 U、V、W、X、Y、Z(根据问题陈述,见图)。请编辑。但是我有想法通过你的方法来解决。泰
    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 2021-10-12
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多