Click here to Skip to main content
14,739,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
and
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() });


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":[  

      ]
   }
]
Posted
Updated 7-Jan-18 7:17am
v2

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#[^]
   
For .Net core, AsEnumerable() is only available from version 3.0 Preview 8 and on.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900