How to Deserialize dataset to list of classes using ASP.NET Core C#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
How to serialise dataset to object in C#? My stored procedure returns a dataset with tables based on these select statements:
select
st.Active, st.CountryCode, st.StateCode, st.StateName,st.GSTCode
from
StateMaster st
select
ct.CountryCode, ct.CountryName, ct.Active
from
CountryMaster ct
select
cm.Active, cm.CityCode, cm.CityName, cm.CountryCode,
cm.CreateDate, cm.ID, cm.StateCode, cm.CreatedBy
from
CityMaster cm
It returns:
{
"Table": [
{
"CountryCode": "91",
"StateCode": "01",
"StateName": "Andhra Pradesh",
"Active": true,
"GSTCode": "37"
},
{
"CountryCode": "91",
"StateCode": "02",
"StateName": "Arunachal Pradesh",
"Active": true,
"GSTCode": "12"
}
],
"Table1": [
{
"CountryCode": "91",
"CountryName": "India",
"Active": true
}
],
"Table2": [
{
"ID": 1113,
"CityCode": "1",
"CityName": " Alipur ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:32.44",
"CreatedBy": "6000013"
},
{
"ID": 1122,
"CityCode": "10",
"CityName": " Bindraban ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:42.437",
"CreatedBy": "6000013"
},
{
"ID": 1212,
"CityCode": "100",
"CityName": " Bapatla ",
"StateCode": "01",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T11:01:21.817",
"CreatedBy": "6000013"
}
]
}
JsonConvert.DeserializeObject<AllClsList>(jsondpl);
returns an empty class.
Please guide me - how can I deserialize or convert dataset directly to an AllClsList
asp.net-web-api asp.net-core
add a comment |
How to serialise dataset to object in C#? My stored procedure returns a dataset with tables based on these select statements:
select
st.Active, st.CountryCode, st.StateCode, st.StateName,st.GSTCode
from
StateMaster st
select
ct.CountryCode, ct.CountryName, ct.Active
from
CountryMaster ct
select
cm.Active, cm.CityCode, cm.CityName, cm.CountryCode,
cm.CreateDate, cm.ID, cm.StateCode, cm.CreatedBy
from
CityMaster cm
It returns:
{
"Table": [
{
"CountryCode": "91",
"StateCode": "01",
"StateName": "Andhra Pradesh",
"Active": true,
"GSTCode": "37"
},
{
"CountryCode": "91",
"StateCode": "02",
"StateName": "Arunachal Pradesh",
"Active": true,
"GSTCode": "12"
}
],
"Table1": [
{
"CountryCode": "91",
"CountryName": "India",
"Active": true
}
],
"Table2": [
{
"ID": 1113,
"CityCode": "1",
"CityName": " Alipur ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:32.44",
"CreatedBy": "6000013"
},
{
"ID": 1122,
"CityCode": "10",
"CityName": " Bindraban ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:42.437",
"CreatedBy": "6000013"
},
{
"ID": 1212,
"CityCode": "100",
"CityName": " Bapatla ",
"StateCode": "01",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T11:01:21.817",
"CreatedBy": "6000013"
}
]
}
JsonConvert.DeserializeObject<AllClsList>(jsondpl);
returns an empty class.
Please guide me - how can I deserialize or convert dataset directly to an AllClsList
asp.net-web-api asp.net-core
add a comment |
How to serialise dataset to object in C#? My stored procedure returns a dataset with tables based on these select statements:
select
st.Active, st.CountryCode, st.StateCode, st.StateName,st.GSTCode
from
StateMaster st
select
ct.CountryCode, ct.CountryName, ct.Active
from
CountryMaster ct
select
cm.Active, cm.CityCode, cm.CityName, cm.CountryCode,
cm.CreateDate, cm.ID, cm.StateCode, cm.CreatedBy
from
CityMaster cm
It returns:
{
"Table": [
{
"CountryCode": "91",
"StateCode": "01",
"StateName": "Andhra Pradesh",
"Active": true,
"GSTCode": "37"
},
{
"CountryCode": "91",
"StateCode": "02",
"StateName": "Arunachal Pradesh",
"Active": true,
"GSTCode": "12"
}
],
"Table1": [
{
"CountryCode": "91",
"CountryName": "India",
"Active": true
}
],
"Table2": [
{
"ID": 1113,
"CityCode": "1",
"CityName": " Alipur ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:32.44",
"CreatedBy": "6000013"
},
{
"ID": 1122,
"CityCode": "10",
"CityName": " Bindraban ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:42.437",
"CreatedBy": "6000013"
},
{
"ID": 1212,
"CityCode": "100",
"CityName": " Bapatla ",
"StateCode": "01",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T11:01:21.817",
"CreatedBy": "6000013"
}
]
}
JsonConvert.DeserializeObject<AllClsList>(jsondpl);
returns an empty class.
Please guide me - how can I deserialize or convert dataset directly to an AllClsList
asp.net-web-api asp.net-core
How to serialise dataset to object in C#? My stored procedure returns a dataset with tables based on these select statements:
select
st.Active, st.CountryCode, st.StateCode, st.StateName,st.GSTCode
from
StateMaster st
select
ct.CountryCode, ct.CountryName, ct.Active
from
CountryMaster ct
select
cm.Active, cm.CityCode, cm.CityName, cm.CountryCode,
cm.CreateDate, cm.ID, cm.StateCode, cm.CreatedBy
from
CityMaster cm
It returns:
{
"Table": [
{
"CountryCode": "91",
"StateCode": "01",
"StateName": "Andhra Pradesh",
"Active": true,
"GSTCode": "37"
},
{
"CountryCode": "91",
"StateCode": "02",
"StateName": "Arunachal Pradesh",
"Active": true,
"GSTCode": "12"
}
],
"Table1": [
{
"CountryCode": "91",
"CountryName": "India",
"Active": true
}
],
"Table2": [
{
"ID": 1113,
"CityCode": "1",
"CityName": " Alipur ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:32.44",
"CreatedBy": "6000013"
},
{
"ID": 1122,
"CityCode": "10",
"CityName": " Bindraban ",
"StateCode": "31",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T10:59:42.437",
"CreatedBy": "6000013"
},
{
"ID": 1212,
"CityCode": "100",
"CityName": " Bapatla ",
"StateCode": "01",
"CountryCode": 91,
"Active": true,
"CreateDate": "2018-07-25T11:01:21.817",
"CreatedBy": "6000013"
}
]
}
JsonConvert.DeserializeObject<AllClsList>(jsondpl);
returns an empty class.
Please guide me - how can I deserialize or convert dataset directly to an AllClsList
asp.net-web-api asp.net-core
asp.net-web-api asp.net-core
edited Jan 3 at 7:16
TanvirArjel
9,84732148
9,84732148
asked Jan 3 at 5:51
vilas jadhavvilas jadhav
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Make Table
, Table1
and Table2
DTO
classes for mapping data as follows:
public class Table
{
public string CountryCode {get; set;}
public string StateCode {get; set;}
public string StateName {get; set;}
public bool Active {get; set;}
public string GSTCode {get; set;}
}
public class Table1
{
public string CountryCode {get; set;}
public string CountryName {get; set;
public bool Active {get; set;}
}
public class Table2
{
public string ID {get; set;}
public string CityCode {get; set;}
public string CityName {get; set;}
public string StateCode {get; set;}
public string ContryCode {get; set;}
public bool Active {get; set;}
public string CreateDate {get; set;}
public string CreatedBy {get; set;}
}
Now write a model class containing all tables as follows:
public class AllTables
{
public List<Table> Tables {get; set;}
public List<Table1> Table1s {get; set;}
public List<Table2> Table2s {get; set;}
}
Now If you received the JsonData through API call then deserialize as follows:
var allTables = JsonConvert.DeserializeObject<AllTables>(yourJsonData);
If the stored procedure that returning the data is in your own application database then do as follows:
Your DbContext
:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbQuery<AllTables> AllTables { get; set; }
}
Then make the query as follows:
var allTables = context.AllTables
.FromSql("EXECUTE dbo.YourStoredProcedure")
.ToList();
For more details: Query Types in EF Core
1
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then clickEdit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
1
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016980%2fhow-to-deserialize-dataset-to-list-of-classes-using-asp-net-core-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Make Table
, Table1
and Table2
DTO
classes for mapping data as follows:
public class Table
{
public string CountryCode {get; set;}
public string StateCode {get; set;}
public string StateName {get; set;}
public bool Active {get; set;}
public string GSTCode {get; set;}
}
public class Table1
{
public string CountryCode {get; set;}
public string CountryName {get; set;
public bool Active {get; set;}
}
public class Table2
{
public string ID {get; set;}
public string CityCode {get; set;}
public string CityName {get; set;}
public string StateCode {get; set;}
public string ContryCode {get; set;}
public bool Active {get; set;}
public string CreateDate {get; set;}
public string CreatedBy {get; set;}
}
Now write a model class containing all tables as follows:
public class AllTables
{
public List<Table> Tables {get; set;}
public List<Table1> Table1s {get; set;}
public List<Table2> Table2s {get; set;}
}
Now If you received the JsonData through API call then deserialize as follows:
var allTables = JsonConvert.DeserializeObject<AllTables>(yourJsonData);
If the stored procedure that returning the data is in your own application database then do as follows:
Your DbContext
:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbQuery<AllTables> AllTables { get; set; }
}
Then make the query as follows:
var allTables = context.AllTables
.FromSql("EXECUTE dbo.YourStoredProcedure")
.ToList();
For more details: Query Types in EF Core
1
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then clickEdit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
1
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
add a comment |
Make Table
, Table1
and Table2
DTO
classes for mapping data as follows:
public class Table
{
public string CountryCode {get; set;}
public string StateCode {get; set;}
public string StateName {get; set;}
public bool Active {get; set;}
public string GSTCode {get; set;}
}
public class Table1
{
public string CountryCode {get; set;}
public string CountryName {get; set;
public bool Active {get; set;}
}
public class Table2
{
public string ID {get; set;}
public string CityCode {get; set;}
public string CityName {get; set;}
public string StateCode {get; set;}
public string ContryCode {get; set;}
public bool Active {get; set;}
public string CreateDate {get; set;}
public string CreatedBy {get; set;}
}
Now write a model class containing all tables as follows:
public class AllTables
{
public List<Table> Tables {get; set;}
public List<Table1> Table1s {get; set;}
public List<Table2> Table2s {get; set;}
}
Now If you received the JsonData through API call then deserialize as follows:
var allTables = JsonConvert.DeserializeObject<AllTables>(yourJsonData);
If the stored procedure that returning the data is in your own application database then do as follows:
Your DbContext
:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbQuery<AllTables> AllTables { get; set; }
}
Then make the query as follows:
var allTables = context.AllTables
.FromSql("EXECUTE dbo.YourStoredProcedure")
.ToList();
For more details: Query Types in EF Core
1
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then clickEdit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
1
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
add a comment |
Make Table
, Table1
and Table2
DTO
classes for mapping data as follows:
public class Table
{
public string CountryCode {get; set;}
public string StateCode {get; set;}
public string StateName {get; set;}
public bool Active {get; set;}
public string GSTCode {get; set;}
}
public class Table1
{
public string CountryCode {get; set;}
public string CountryName {get; set;
public bool Active {get; set;}
}
public class Table2
{
public string ID {get; set;}
public string CityCode {get; set;}
public string CityName {get; set;}
public string StateCode {get; set;}
public string ContryCode {get; set;}
public bool Active {get; set;}
public string CreateDate {get; set;}
public string CreatedBy {get; set;}
}
Now write a model class containing all tables as follows:
public class AllTables
{
public List<Table> Tables {get; set;}
public List<Table1> Table1s {get; set;}
public List<Table2> Table2s {get; set;}
}
Now If you received the JsonData through API call then deserialize as follows:
var allTables = JsonConvert.DeserializeObject<AllTables>(yourJsonData);
If the stored procedure that returning the data is in your own application database then do as follows:
Your DbContext
:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbQuery<AllTables> AllTables { get; set; }
}
Then make the query as follows:
var allTables = context.AllTables
.FromSql("EXECUTE dbo.YourStoredProcedure")
.ToList();
For more details: Query Types in EF Core
Make Table
, Table1
and Table2
DTO
classes for mapping data as follows:
public class Table
{
public string CountryCode {get; set;}
public string StateCode {get; set;}
public string StateName {get; set;}
public bool Active {get; set;}
public string GSTCode {get; set;}
}
public class Table1
{
public string CountryCode {get; set;}
public string CountryName {get; set;
public bool Active {get; set;}
}
public class Table2
{
public string ID {get; set;}
public string CityCode {get; set;}
public string CityName {get; set;}
public string StateCode {get; set;}
public string ContryCode {get; set;}
public bool Active {get; set;}
public string CreateDate {get; set;}
public string CreatedBy {get; set;}
}
Now write a model class containing all tables as follows:
public class AllTables
{
public List<Table> Tables {get; set;}
public List<Table1> Table1s {get; set;}
public List<Table2> Table2s {get; set;}
}
Now If you received the JsonData through API call then deserialize as follows:
var allTables = JsonConvert.DeserializeObject<AllTables>(yourJsonData);
If the stored procedure that returning the data is in your own application database then do as follows:
Your DbContext
:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbQuery<AllTables> AllTables { get; set; }
}
Then make the query as follows:
var allTables = context.AllTables
.FromSql("EXECUTE dbo.YourStoredProcedure")
.ToList();
For more details: Query Types in EF Core
edited Jan 3 at 7:08
answered Jan 3 at 7:03
TanvirArjelTanvirArjel
9,84732148
9,84732148
1
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then clickEdit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
1
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
add a comment |
1
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then clickEdit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
1
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
1
1
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then click
Edit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
If you want to generate C# classes out from the json returned in Visual Studio, you can also copy all Json then click
Edit > Paste Special > Paste JSON As Classes
– jwweiler
Jan 3 at 10:23
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
@jwweiler Thank you! I shall try.
– TanvirArjel
Jan 3 at 10:25
1
1
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
it might not mimick the exact functionality you wrote, but is very helpful to quickly just see what VS thinks of the JSON :)
– jwweiler
Jan 3 at 10:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016980%2fhow-to-deserialize-dataset-to-list-of-classes-using-asp-net-core-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown