将数据表转换为无法在ASP.NET核心2.0中工作的asenumerable [英] Convert datatable to asenumerable not working in ASP.NET core 2.0

查看:74
本文介绍了将数据表转换为无法在ASP.NET核心2.0中工作的asenumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Asp.net代码2.0并迁移我的代码。并且AsEnumerable()在.Net Core中不起作用。



我的数据来自表格

i just started with Asp.net code 2.0 and migrating my code. and AsEnumerable() is not working in .Net Core.

my data is coming from table where

api_module

api_resource_name

是列



are the columns

var result = ds.Tables[0].AsEnumerable().GroupBy(x => x.Field<string>("api_module")).Select(x => new { api_module = x.Key, api_resource_name = x.Where(z => z.Field<string>("api_resource_name") != null).Select(z => z.Field<string>("api_resource_name")).ToList() });





我的尝试:



i尝试使用for循环,但无法解决我的问题。



我所需的json格式是





What I have tried:

i have tried using the for loop but could not able to solve my issue.

my required json format is

[  
   {  
      "api_module":"approvals",
      "api_resource_name":[  

      ]
   },
   {  
      "api_module":"bpsusers",
      "api_resource_name":[  
         "alternateapproverfor",
         "alternateapprovers"
      ]
   },
   {  
      "api_module":"commodities",
      "api_resource_name":[  

      ]
   },
   {  
      "api_module":"commoditygroups",
      "api_resource_name":[  

      ]
   },
   {  
      "api_module":"companies",
      "api_resource_name":[  
         "addresses",
         "bids",
         "commitments",
         "commodities",
         "contacts",
         "evaluations",
         "financialinfo",
         "interests",
         "licenses",
         "performanceratings",
         "profisydocuments",
         "profisyfolders",
         "projectinterests",
         "projects",
         "qualifications",
         "relationships",
         "shopload"
      ]
   },
   {  
      "api_module":"costcodes",
      "api_resource_name":[  

      ]
   },
   {  
      "api_module":"currencies",
      "api_resource_name":[  
         "rates"
      ]
   },
   {  
      "api_module":"dataexchangenew",
      "api_resource_name":[  

      ]
   }
]

推荐答案

正如MSDN文档所述 DataTableExtensions.AsEnumerable Method(DataTable)(System.Data) [ ^ ]可用。



请注意,数据表数据和JSON数据之间的转换不能直接访问。您必须定义一组类才能序列化和反序列化JSON数据(强烈建议)。有关详细信息,请参阅:

JSON序列化| Microsoft Docs [ ^ ]

如何:序列化和反序列化JSON数据| Microsoft Docs [ ^ ]



此外,请参阅:

在C#中将数据表转换为json [ ^ ]

3种转换方法ASP.NET C#中的DataTable到JSON字符串 [ ^ ]
As MSDN documentation states a DataTableExtensions.AsEnumerable Method (DataTable) (System.Data)[^] is available since .NET framework 3.5.

Note that conversion between datatable data and JSON data isnt't accessible directly. You have to define set of classes to be able to serialize and deserialize JSON data (strongly recommended). For further details, please see:
JSON Serialization | Microsoft Docs[^]
How to: Serialize and Deserialize JSON Data | Microsoft Docs[^]

Also, please see:
Convert datatable to json in C#[^]
3 Ways to Convert DataTable to JSON String in ASP.NET C#[^]


这篇关于将数据表转换为无法在ASP.NET核心2.0中工作的asenumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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