leo*_*ora 3 c# datatable dictionary
我有很多情况下我会进行查询并获取数据表并将其转换为:
Dictionary<string, string>
Run Code Online (Sandbox Code Playgroud)
要么
Dictionary<int, int>
Run Code Online (Sandbox Code Playgroud)
要么
Dictionary<string, [Someobject]>
Run Code Online (Sandbox Code Playgroud)
我可以使用循环创建自己的函数,但我想看看是否有任何LINQ方式将数据表转换为字典,只是为键和值传递2个lambdas(类似于将IEnumerable转换为字典所能做到的)
您可以使用AsEnumerable和ToDictionary:
例如,如果您想要一个字典,其中键来自col1,值来自col2:
table.AsEnumerable()
.ToDictionary<int, string>(row => row.Field<int>(col1),
row => row.Field<string>(col2));
Run Code Online (Sandbox Code Playgroud)