C# 将字典列表作为数据集获取的扩展方法?
我正在尝试将字典对象列表强制转换为数据集。该列表来自JSON解析器。我决定借此机会学习扩展方法 单个字典的扩展方法可以工作,但字典列表的方法对我来说“看起来”不太合适,主要是因为调用变为C# 将字典列表作为数据集获取的扩展方法?,c#,generics,extension-methods,C#,Generics,Extension Methods,我正在尝试将字典对象列表强制转换为数据集。该列表来自JSON解析器。我决定借此机会学习扩展方法 单个字典的扩展方法可以工作,但字典列表的方法对我来说“看起来”不太合适,主要是因为调用变为 DataSet myExampleDataSet = myExampleDictionary.ToDataSet<Dictionary<string,string>,string,string>(); DataSet myExampleDataSet=myExampleDictiona
DataSet myExampleDataSet = myExampleDictionary.ToDataSet<Dictionary<string,string>,string,string>();
DataSet myExampleDataSet=myExampleDictionary.ToDataSet();
我错过什么了吗?真的要这么复杂吗?我应该把Dictionary.ToDataSet方法扔进foreach吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
//fixed code below
namespace TT.Utils
{
public static class DictionaryExtensions
{
/// <summary>
/// Dictionary to DataSet
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="currentDictionary"></param>
/// <returns></returns>
public static DataSet ToDataSet<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary)
{
DataSet exportedDataSet = new DataSet();
DataTable exportedDataTable = exportedDataSet.Tables.Add();
foreach (TKey key in currentDictionary.Keys)
{
exportedDataTable.Columns.Add(key.ToString());
}
DataRow newRow = exportedDataTable.NewRow();
foreach (KeyValuePair<TKey, TValue> entry in currentDictionary)
{
string key = entry.Key.ToString();
string val = string.Empty;
if (entry.Value != null)
{
val = entry.Value.ToString();
}
newRow[key] = val;
}
exportedDataSet.Tables[0].Rows.Add(newRow);
return exportedDataSet;
}
/// <summary>
/// List of dictionaries to dataset
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="currentList"></param>
/// <returns></returns>
public static DataSet ToDataSet<TKey,TValue>(this IList<Dictionary<TKey,TValue> currentList)
{
DataSet exportedDataSet = new DataSet();
DataTable exportedDataTable = exportedDataSet.Tables.Add();
foreach (Dictionary<TKey, TValue> currentDictionary in currentList.Cast<Dictionary<TKey,TValue>>())
{
foreach (TKey key in currentDictionary.Keys)
{
if (!exportedDataTable.Columns.Contains(key.ToString()))
exportedDataTable.Columns.Add(key.ToString());
}
DataRow newRow = exportedDataTable.NewRow();
foreach (KeyValuePair<TKey, TValue> entry in currentDictionary)
{
string key = entry.Key.ToString();
string val = string.Empty;
if (entry.Value != null)
{
val = entry.Value.ToString();
}
newRow[key] = val;
}
exportedDataSet.Tables[0].Rows.Add(newRow);
}
return exportedDataSet;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用系统集合;
//下面是固定代码
名称空间TT.Utils
{
公共静态类字典扩展
{
///
///字典到数据集
///
///
///
///
///
公共静态数据集ToDataSet(此IDictionary currentDictionary)
{
DataSet exportedDataSet=新数据集();
DataTable exportedDataTable=exportedDataSet.Tables.Add();
foreach(currentDictionary.Keys中的TKey键)
{
exportedDataTable.Columns.Add(key.ToString());
}
DataRow newRow=exportedDataTable.newRow();
foreach(currentDictionary中的KeyValuePair条目)
{
string key=entry.key.ToString();
string val=string.Empty;
if(entry.Value!=null)
{
val=entry.Value.ToString();
}
newRow[key]=val;
}
exportedDataSet.Tables[0].Rows.Add(newRow);
返回导出的数据集;
}
///
///要创建数据集的词典列表
///
///
///
///
///
公共静态数据集ToDataSet(此IList调用泛型方法时,通常不需要显式定义使用的泛型类型参数。这些类型将由调用的参数类型暗示。如果存在歧义,编译器将通知您
e、 g
这里只有两种未知类型,TKey
和TValue
,这将像往常一样隐含
IList<IDictionary<string, string>> myList = ...;
DataSet myDataSet = myList.ToDataSet();
// equivalent to: myList.ToDataSet<string, string>();
IDictionary<string, int> anotherList = ...;
DataSet anotherDataSet = anotherList.ToDataSet();
// equivalent to: anotherList.ToDataSet<string, int>();
IList myList=。。。;
DataSet myDataSet=myList.ToDataSet();
//等效于:myList.ToDataSet();
IDictionary anotherList=。。。;
DataSet anotherDataSet=anotherList.ToDataSet();
//等效于:anotherList.ToDataSet();
调用泛型方法时,通常不需要显式定义所使用的泛型类型参数。这些类型将由调用的参数类型暗示。如果存在歧义,编译器将通知您
e、 g
这里只有两种未知类型,TKey
和TValue
,这将像往常一样隐含
IList<IDictionary<string, string>> myList = ...;
DataSet myDataSet = myList.ToDataSet();
// equivalent to: myList.ToDataSet<string, string>();
IDictionary<string, int> anotherList = ...;
DataSet anotherDataSet = anotherList.ToDataSet();
// equivalent to: anotherList.ToDataSet<string, int>();
IList myList=。。。;
DataSet myDataSet=myList.ToDataSet();
//等效于:myList.ToDataSet();
IDictionary anotherList=。。。;
DataSet anotherDataSet=anotherList.ToDataSet();
//等效于:anotherList.ToDataSet();
Jeff M关于为第一个方法指定泛型类型参数的冗余性是正确的
不幸的是,第二个方法的签名使得编译器无法推断类型参数。一个选项是将签名更改为:
public static DataSet ToDataSet<TKey,TValue>
(this IList<Dictionary<TKey,TValue>> currentList)
Jeff M关于为第一个方法指定泛型类型参数的冗余性是正确的
不幸的是,第二个方法的签名使得编译器无法推断类型参数。一个选项是将签名更改为:
public static DataSet ToDataSet<TKey,TValue>
(this IList<Dictionary<TKey,TValue>> currentList)
您的方法调用不需要指定以下类型:
IDictionary<string, int> test = new Dictionary<string, int>();
// later on.
test.ToDataSet(); // is already valid
IDictionary test=new Dictionary();
//后来。
test.ToDataSet();//已有效
方法调用不需要指定类型,即:
IDictionary<string, int> test = new Dictionary<string, int>();
// later on.
test.ToDataSet(); // is already valid
IDictionary test=new Dictionary();
//后来。
test.ToDataSet();//已有效
那么列表类型将成为第二个方法中唯一的类型参数吗?以上代码就是解决方案,再加上对第二个方法的更改:签名成为公共静态数据集ToDataSet(此IList currentList)//谢谢Jeff!那么列表类型将成为第二个方法中唯一的类型参数?上面的代码就是解决方案,再加上对我的第二个方法的更改:签名成为公共静态数据集ToDataSet(此IList currentList)//谢谢杰夫!你的ToDataSet
在
中放了三种类型。它真的编译了吗?你的ToDataSet
在
中放了三种类型。它真的编译了吗?