将数据表转换为字典 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19712958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transform a DataTable into Dictionary C#
提问by Maximus Decimus
I want to know how to transform a DataTable into a Dictionary. I did something like this.
我想知道如何将 DataTable 转换为 Dictionary。我做了这样的事情。
using System.Linq;
internal Dictionary<string,object> GetDict(DataTable dt)
{
return dt.AsEnumerable()
.ToDictionary<string, object>(row => row.Field<string>(0),
row => row.Field<object>(1));
}
But I get:
但我得到:
System.Data.EnumerableRowCollection does not contains a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Parallel.Enumerable.ToDictionary(System.Linq.ParallelQuery, System.Func, System.Collections.Generic.IEqualityComrparer)' has some invalid argumentsch
System.Data.EnumerableRowCollection 不包含“ToDictionary”的定义和最佳扩展方法重载“System.Linq.Parallel.Enumerable.ToDictionary(System.Linq.ParallelQuery, System.Func, System.Collections.Generic.IEqualityComrparer)”有一些无效的论点
How can I resolve this?
我该如何解决这个问题?
Thanks
谢谢
采纳答案by cadrell0
The generic method ToDictionary
has 3 parameters. You left one off, so it doesn't know what to do. If you want to specify all of the parameters, it would be <DataRow, string, object>
.
泛型方法ToDictionary
有 3 个参数。你留下了一个,所以它不知道该怎么做。如果要指定所有参数,则为<DataRow, string, object>
.
internal Dictionary<string,object> GetDict(DataTable dt)
{
return dt.AsEnumerable()
.ToDictionary<DataRow, string, object>(row => row.Field<string>(0),
row => row.Field<object>(1));
}
Of course, if you leave them off, the compiler is able to infer the types, so you don't get the error.
当然,如果您不使用它们,编译器可以推断类型,因此您不会收到错误消息。
回答by Maximus Decimus
I found the solution but don't know why. I edited my Question completing the code just for make it clear what I was doing an I changed to this
我找到了解决方案,但不知道为什么。我编辑了我的问题来完成代码只是为了清楚我在做什么我改变了这个
internal Dictionary<string, object> GetDict(DataTable dt)
{
Dictionary<String, Object> dic = dt.AsEnumerable().ToDictionary(row => row.Field<String>(0), row => row.Field<Object>(1));
return dic;
}
回答by Kevin
ToDictionary is expecting the IEnumberable<T>
as the first type... you were telling it that it was a string which is wrong it's IEnumerable<DataRow>
ToDictionary 期望IEnumberable<T>
作为第一种类型......你告诉它这是一个错误的字符串IEnumerable<DataRow>
It's getting confused by you specifying the types... try this...
您指定类型会让人感到困惑......试试这个......
internal Dictionary<string,object> GetDict(DataTable dt)
{
return dt.AsEnumerable()
.ToDictionary(row => row.Field<string>(0),
row => row.Field<object>(1));
}
回答by terrybozzio
i think this will help you:
我认为这会帮助你:
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Rows.Add(1, "first");
dt.Rows.Add(2, "second");
var dictionary = dt.Rows.OfType<DataRow>().ToDictionary(d => d.Field<string>(0), v => v.Field<object>(1));
回答by ParPar
All the previos answers didn't help me, so I did this:
所有以前的答案都没有帮助我,所以我这样做了:
myList = dt.AsEnumerable()
.ToDictionary<DataRow, string, string>(row => row[0].ToString(),
row => row[1].ToString());
and it worked great!
效果很好!
回答by Yakir Manor
i prefer this method:
我更喜欢这种方法:
public static List<Dictionary<string, string>> GetDataTableDictionaryList(DataTable dt)
{
return dt.AsEnumerable().Select(
row => dt.Columns.Cast<DataColumn>().ToDictionary(
column => column.ColumnName,
column => row[column].ToString()
)).ToList();
}
the reason why is because this code can also deal with Booleans or other data types by calling the ToString method.
原因是因为这段代码也可以通过调用 ToString 方法来处理布尔值或其他数据类型。
Notice this returns a list of dictionaries, you can modify it to a dictionary of dictionaries if you have key for each row.
请注意,这将返回一个字典列表,如果您有每行的键,则可以将其修改为字典字典。
iterate over a bool column might look like so:
遍历 bool 列可能如下所示:
var list = GetDataTableDictionaryList(dt);
foreach (var row in list)
{
if (row["Selected"].Equals("true", StringComparison.OrdinalIgnoreCase))
{
// do something
}
}
回答by Pawel Cioch
Given solutions assume only 2 columns. In case you want multi column representation, you need a list of dictionary
给定的解决方案假设只有 2 列。如果你想要多列表示,你需要一个字典列表
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Rows.Add(1, "first", "A");
dt.Rows.Add(2, "second", "B");
var dictTable = DataTableToDictionaryList(dt);
var rowCount = dictTable.Count;
var colCount = dictTable[0].Count;
//Linq version
var dictTableFromLinq = dt.AsEnumerable().Select(
// ...then iterate through the columns...
row => dt.Columns.Cast<DataColumn>().ToDictionary(
// ...and find the key value pairs for the dictionary
column => column.ColumnName, // Key
column => row[column] as string // Value
)
).ToList();
}
public static List<Dictionary<string, object>> DataTableToDictionaryList(DataTable dt)
{
var result = new List<Dictionary<string, object>>();
//or var result = new List<Dictionary<string, string>>();
foreach (DataRow row in dt.Rows)
{
var dictRow = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
dictRow.Add(col.ColumnName, row[col]);
//or dictRow.Add(col.ColumnName, row[col].ToString());
}
result.Add(dictRow);
}
return result;
}
}
回答by fcm
Visual basic
Using sql to load a datatable and create a dictionary
Visual basic
使用sql加载数据表并创建字典
Dim SalesRep As New System.Collections.Generic.Dictionary(Of String, String)(StringComparer.InvariantCultureIgnoreCase)
Using tbl As New Data.DataTable("SalesRep")
SqlCommand.CommandText = "Select Initial,FullName from QB_SalesRep"
tbl.Load(SqlCommand.ExecuteReader())
' --- Option ONE use the array to iterate
Array.ForEach(tbl.Rows.Cast(Of Data.DataRow).ToArray(),
Sub(f)
SalesRep.Add(f.ItemAsString("Initial"), f.ItemAsString("FullName"))
End Sub)
' --- Option TWO use plain linq to create the dictionary
SalesRep = tbl.Rows _
.Cast(Of Data.DataRow) _
.AsEnumerable() _
.ToDictionary(Function(f) f.Item("Initial").toString(), Function(f) f.Item("FullName").toString)
End Using