相关疑难解决方法(0)

将IEnumerable转换为DataTable

有没有一种很好的方法将IEnumerable转换为DataTable?

我可以使用反射来获取属性和值,但这似乎有点低效,是否有内置的东西?

(我知道这些例子:ObtainDataTableFromIEnumerable)

编辑:
这个问题通知我处理空值的问题.
我在下面写的代码正确处理空值.

public static DataTable ToDataTable<T>(this IEnumerable<T> items) {  
    // Create the result table, and gather all properties of a T        
    DataTable table = new DataTable(typeof(T).Name); 
    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);  

    // Add the properties as columns to the datatable
    foreach (var prop in props) { 
        Type propType = prop.PropertyType; 

        // Is it a nullable type? Get the underlying type 
        if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) 
            propType = new NullableConverter(propType).UnderlyingType;  

        table.Columns.Add(prop.Name, propType); 
    }  

    // …
Run Code Online (Sandbox Code Playgroud)

c# datatable ienumerable

63
推荐指数
4
解决办法
10万
查看次数

使用带有F#的SqlBulkCopy在SQL中导出矩阵

我想将大量数据从F#传输到SQL表.基本上我的F#代码创建了一个包含三列(UserID, ProductID and price)和N行的矩阵.我想"复制/掌握它"到数据库中我尝试了几个选项,但最后,从F#传输数据真的很慢(10000行左右大约一小时).

感谢前一个问题的答案如何在F#中包含存储过程,解决此问题的一个有趣方法是使用SqlBulkCopy.

SqlBulkCopy需要一个数据库类型的WritetoServer方法,但我没有找到任何现有的代码或简单的方法将矩阵转换为数据库.你有什么建议或想法吗?

sql f# matrix sqlbulkcopy

2
推荐指数
1
解决办法
519
查看次数

标签 统计

c# ×1

datatable ×1

f# ×1

ienumerable ×1

matrix ×1

sql ×1

sqlbulkcopy ×1