Convert generic List/Enumerable to DataTable?

后端 未结 27 2142
臣服心动
臣服心动 2020-11-21 23:20

I have few methods that returns different Generic Lists.

Exists in .net any class static method or whatever to convert any list into a datatable? The only thing tha

27条回答
  •  感情败类
    2020-11-21 23:44

    A 2019 answer if you're using .NET Core - use the Nuget ToDataTable library. Advantages:

    • Better performance than FastMember
    • Also creates structured SqlParameters for use as SQL Server Table-Valued Parameters

    Disclaimer - I'm the author of ToDataTable

    Performance - I span up some Benchmark .Net tests and included them in the ToDataTable repo. The results were as follows:

    Creating a 100,000 Row Datatable:

                               MacOS         Windows
    Reflection                 818.5 ms      818.3 ms
    FastMember from           1105.5 ms      976.4 ms
     Mark's answer
    Improved FastMember        524.6 ms      456.4 ms
    ToDataTable                449.0 ms      376.5 ms
    

    The FastMember method suggested in Marc's answer seemed to perform worse than Mary's answer which used reflection, but I rolled another method using a FastMember TypeAccessor and it performed much better. Nevertheless the ToDataTable package outperformed the lot.

提交回复
热议问题