[英]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.