将DataTable批量反序列化为类的对象 [英] Bulk Deserializing of DataTable to Object of Class

查看:90
本文介绍了将DataTable批量反序列化为类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据上传到API,格式类似于 

I am uploading data to an API and the format is something like 

{
  "fielda": "conststring",
  "conversions": [
    conversions Resource
  ]
}

where conversions is defined as

{
  "kind": "conststring#conversion",
  "configurationId": long,
  "stringid": string,
  "customVariables": [
    {
      "kind": "conststring#customFloodlightVariable",
      "type": string,
      "value": string
    }
  ]
}

 

包含要添加的样本虚拟值的示例代码如下所示

a Sample code with sample dummy values to add looks like this

            Conversion conversion = new Conversion();
            conversion.ConfigurationId = ConfigurationId; //mandatory field
    
            conversion.stringid= "stringabc";  // One of the must use Optional fields

            CustomVariable A=new CustomVariable ();
            A.Kind = "conststring#customVariable";
            A.Type = "A20";
            A.Value = "0.0192186832427979"; 

            CustomVariable B= new CustomVariable();
            B.Kind = "dfareporting#customFloodlightVariable";
            B.Type = "A21";
            B.Value = "15235.0840575384";

            conversion.CustomVariables = new List<CustomVariable>();

            conversion.CustomVariables.Add(A);
            conversion.CustomVariables.Add(B);

这工作正常,但我现在尝试通过从SQL数据库读取来分配这些值,我正在尝试找到性能最高的版本,它不会一次遍历数据集一行。有没有办法实现这个目标? 

This works ok but I am now trying to assign these values by reading from a SQL database and I am trying to find the most performant version, which does not iterate through the dataset one row at a time. Is there a way to accomplish this? 

我查看了https://codereview.stackexchange.com/questions/30714/converting-datatable-to-list-of- class

I have looked at https://codereview.stackexchange.com/questions/30714/converting-datatable-to-list-of-class

但我有2个问题:

1。当我有数千行时,这真的很有效吗?

1. Is that really performant as and when I have thousands of rows?

2。如果第1点的答案为是,我如何初始化嵌套列表列表"customVariable"?在转换类中?自定义变量将来自数据库中的不同字段。

2. If the answer to point 1 is yes, how do I initialize the nested list list "customVariable" within the Conversion class? The Custom variables would come from different fields in the database.




推荐答案

我有一个不同的建议:您的数据看起来像JSON。您是否考虑过使用JSON序列化程序进行转换?我倾向于使用NewtonSoft中的那个(你可以将它作为NuGet包添加到你的项目中),但还有很多其他的。你不需要担心性能,它会像你手动编码的任何东西一样快。

I have a different suggestion: Your data looks like JSON. Have you considered using a JSON serializer to do the conversion? I tend to use the one from NewtonSoft (you can add it to your project as a NuGet package) but there are many others. You needn't worry about the performance, it will be as fast as anything you code manually.

还有另一种选择。您提到数据来自SQL数据库。如果您使用的是SQL Server 2017(或可以升级到它),它现在支持JSON数据类型,并能够以此格式保存和处理数据。使用巧妙编写的
SQL语句,您可以将关系数据转换为JSON和从JSON转换。这可以直接在SQL端为您提供所需的数据,这可能比首先将关系数据传递到客户端然后在那里序列化更快。

There is also another alternative. You mention that the data comes from a SQL database. If you are using SQL Server 2017 (or can upgrade to it), it now supports the JSON data type and is able to save and process data in this format. With a cleverly written SQL statement you can convert relational data to and from JSON. This can give you the data that you want directly on the SQL side, which is likely to be faster than first bringing relational data to the client side and then serializing it over there.


这篇关于将DataTable批量反序列化为类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆