How to convert dataset result to class objectAAshok5yAsked Jan 5, 2021, 4:56 AM ESTSolvedShareLinkedInXCopy linkSaveHi All,How to convert dataset result columns into c# class object. Thank You
How to convert dataset result to class objectAAshok5yAsked Jan 5, 2021, 4:56 AM ESTSolvedShareLinkedInXCopy linkSaveHi All,How to convert dataset result columns into c# class object. Thank You
Sachin SinghRank #66 · 26.2K reputation5y agoPosted Jan 5, 2021, 5:14 AM EST✓ Accepted answer+2simply convert it to Enumarable and then loop through it using linqvar ContactList= ds.Tables[0].AsEnumerable() .Select(dataRow => new Contact() { Name = dataRow.Field<string>("Name"), PhoneNumber= dataRow.Field<int>("PhoneNumber") }).ToList(); +2
Sachin SinghRank #66 · 26.2K reputation5y agoPosted Jan 5, 2021, 5:41 AM EST+2i have just given you an exampleif you datatable contains information about employee then you need to create a class with employee and then take its object for example var EmployeeList= ds.Tables[0].AsEnumerable() .Select(dataRow => new Employee() { Name = dataRow.Field<string>("Name"), // dataset col 1 PhoneNumber= dataRow.Field<int>("PhoneNumber") //col 2 }).ToList(); public class Employee { public string Name {get;set} public int PhoneNumber {get;set} } +2
AANURAG30 reputation5y agoPosted Jan 5, 2021, 1:21 PM EST+2string data = JsonConvert.SerializeObject(ds.Tables[0]);var EmployeeList = JsonConvert.DeserializeObject(data); public class Employee { public string Name {get;set} public int PhoneNumber {get;set} } +2AAshokRank #2,671 · 521 reputation5y agoPosted Jan 5, 2021, 1:41 PM EST+2Hi TQ U its working now but getting some convertion problems could u pls help me out as my json is { "total_cube": 103802.0,}using https://json2csharp.com i converted to classes public class Root { public double total_cube { get; set; } } in c#var EmployeeList= ds.Tables[0].AsEnumerable() .Select(dataRow => new Employee() { Name = dataRow.Field<string>("Name"), // dataset col 1 total_cube= dataRow.Field<double>("total_cube") //col 2 }).ToList(); how can i get value like this 103802+2AANURAG30 reputation5y agoPosted Jan 5, 2021, 2:01 PM EST+2 public ActionResult ConvertToClassObject() { return View(); } [HttpPost] public ActionResult ConvertToClassObject(string data) { Root r1 = JsonConvert.DeserializeObject(data); double get_total_cube = r1.total_cube; return View(); } public class Root { public double total_cube { get; set; } } @{ ViewBag.Title = "ConvertToClassObject";}ConvertToClassObject Submit function send_data() { debugger; var d1 = { "total_cube": 103802.0, }; var d1_str = JSON.stringify(d1); document.getElementById("data").value = d1_str; } +2Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AAshokRank #2,671 · 521 reputation5y agoPosted Jan 5, 2021, 1:41 PM EST+2Hi TQ U its working now but getting some convertion problems could u pls help me out as my json is { "total_cube": 103802.0,}using https://json2csharp.com i converted to classes public class Root { public double total_cube { get; set; } } in c#var EmployeeList= ds.Tables[0].AsEnumerable() .Select(dataRow => new Employee() { Name = dataRow.Field<string>("Name"), // dataset col 1 total_cube= dataRow.Field<double>("total_cube") //col 2 }).ToList(); how can i get value like this 103802+2
AANURAG30 reputation5y agoPosted Jan 5, 2021, 2:01 PM EST+2 public ActionResult ConvertToClassObject() { return View(); } [HttpPost] public ActionResult ConvertToClassObject(string data) { Root r1 = JsonConvert.DeserializeObject(data); double get_total_cube = r1.total_cube; return View(); } public class Root { public double total_cube { get; set; } } @{ ViewBag.Title = "ConvertToClassObject";}ConvertToClassObject Submit function send_data() { debugger; var d1 = { "total_cube": 103802.0, }; var d1_str = JSON.stringify(d1); document.getElementById("data").value = d1_str; } +2
Sachin SinghRank #66 · 26.2K reputationPosted Jan 5, 2021, 5:14 AM EST
Sachin SinghRank #66 · 26.2K reputationPosted Jan 5, 2021, 5:41 AM EST
ANURAG30 reputationPosted Jan 5, 2021, 1:21 PM EST
AshokRank #2,671 · 521 reputationPosted Jan 5, 2021, 1:41 PM EST
ANURAG30 reputationPosted Jan 5, 2021, 2:01 PM EST
ConvertToClassObject
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts