简体   繁体   English

如何将数据表转换为List <long,List<keyvaluepair<string,string> &gt;&gt;在C#中

[英]how to convert a datatable to List<long,List<keyvaluepair<string,string>>> in C#

I have a datatable with three columns says, ID, Name and Value. 我有一个包含三列的数据表,ID,Name和Value。

I want to convert this datatable into List<long,List<keyvaluepair<string,string>>> . 我想将此数据List<long,List<keyvaluepair<string,string>>>转换为List<long,List<keyvaluepair<string,string>>>

The only trick is ID is foreign ket from another table. 唯一的技巧是ID是来自另一个表的外来的。 so it could have repeatative values. 所以它可能具有重复价值。

I want to do it in C#. 我想用C#做。

Thanks in advance. 提前致谢。


no, not dictionaly.....actualy I have two classes says, definded as.... 不,不是dictionaly .....actaly我有两个课说,定义为....

public class RunParameters
    {
        public long RunId { get; set; }
        public List<WorkflowParameter<string>> WorkflowParameters { get; set; }

        public RunParameters()
        {
            WorkflowParameters = new List<WorkflowParameter<string>>();
        }
    }

 public struct WorkflowParameter<T>
    {
        public string ParameterName
        { get; set; }
        public T ParameterValue
        { get; set; }
    }

I want to convert the datable into List. 我想将数据转换为List。

I cannot use dictionary since I want to serialize the this List to send it across the network.... 我不能使用字典,因为我想序列化这个List以通过网络发送....

Group items in your query by ID and project each item from group to KeyValuePair : ID对您的查询中的项目进行分组,并将每个项目从组计划到KeyValuePair

(from r in table.AsEnumerable()
 group r by r.Field<long>("ID") into g
 select g.Select(i => new KeyValuePair<string, string>(
                      i.Field<string>("Name"), i.Field<string>("Value"))
         .ToList()).ToList()

Or completely with method syntax: 或完全使用方法语法:

 table.AsEnumerable()
      .GroupBy(r => r.Field<long>("ID"))
      .Select(g => g.Select(r => new KeyValuePair<string, string>(
                          r.Field<string>("Name"), r.Field<string>("Value"))
                    .ToList())
      .ToList() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换清单 <KeyValuePair<string, decimal> &gt;列表=新列表 <KeyValuePair<string, decimal> &gt;(); 到DataTable? - How to convert List<KeyValuePair<string, decimal>> list = new List<KeyValuePair<string, decimal>>( ); to DataTable? 将定界字符串转换为KeyValuePair列表 <string,string> 在C#中 - Convert a delimited string to a List of KeyValuePair<string,string> in C# 如何将数据表转换为列表 <String> 在C#中 - how convert DataTable to List<String> in C# 排序列表 <KeyValuePair<string, double> &gt;在c#中 - Sort List<KeyValuePair<string, double>> in c# c#排序列表 <KeyValuePair<int, string> &gt; - c# Sorting a List<KeyValuePair<int, string>> 如何转换字典 <string, List<object> &gt;进入C#中的DataTable? - How to convert Dictionary<string, List<object>> into DataTable in C#? 如何将数据表转换为字典 <string, list<object> &gt;在C#中? - How to convert a datatable to dictionary<string, list<object>> in C#? C#从列表中获取键和值<KeyValuePair<string, string> - C# get keys and values from List<KeyValuePair<string, string> 如何 select 列表中的键值对列表<list<keyvaluepair<string,double> >> 并修改它们的值? C# </list<keyvaluepair<string,double> - How to select list of keyvaluepairs in List<List<keyvaluepair<string,double>>> and modify their values ? C# 映射列表<KeyValuePair<string,int> &gt; 在 C# 中自定义对象 - Mapping List<KeyValuePair<string,int>> to custom object in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM