Linq表达式将数据表转换为< Key,List< Values>>的字典 [英] Linq Expression to Turn DataTable to Dictionary of <Key, List<Values>>

查看:75
本文介绍了Linq表达式将数据表转换为< Key,List< Values>>的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换形式的DataTable

I am trying to convert a DataTable of the form

Key  Value
1    A
1    B
1    C
2    X
2    Y

去字典

1 [A,B,C]
2 [X,Y]

我正在使用的lambda表达式是

The lambda expression I am using is

GetTable("..sql..").AsEnumerable().
    .Select(r => new {Key = r.Field<int>("Key"), Val = r.Field<string>("Value")})
    .GroupBy(g => g.Key)
    .ToDictionary(a => a.Key, a => String.Join(",", a.Value))

但是失败,因为无法将lambda表达式转换为类型'System.Collections.Generic.IEqualityComparer',因为它不是委托类型"

But it fails with "Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type"

我该怎么做?

推荐答案

做到这一点:

GetTable("..sql..").AsEnumerable().
    .Select(r => new {Key = r.Field<int>("Key"), Val = r.Field<string>("Value")})
    .GroupBy(g => g.Key)
    .ToDictionary(a => a.Key, a => String.Join(",", a.Select(x => x.Value).ToList()))

这篇关于Linq表达式将数据表转换为&lt; Key,List&lt; Values&gt;&gt;的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆