How to retreive all the properties of an object which is inside another object with C# code?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have these three tables in SQL Server:



Evaluations (id, textreview, stars, userId, BookId) 
Books (id, title, ..., userId)
Users (id, name, ...)


And I have this code to get data from my tables:



public List<Evaluations> GetAllEvaluationsPerBook()

string sqlCommand = "SELECT DISTINCT b.Title,ev.TextReview,ev.Stars, b.Id " +
"FROM[Services.BookDbContext].[dbo].[Evaluations] as ev, " +
"[Services.BookDbContext].[dbo].Books as b " +
"WHERE b.Id = ev.Book_Id";

using (var context = new BookDbContext())

var evaluation = context.Evaluation.SqlQuery(sqlCommand).ToList();

return evaluation;




Now I'm creating a WebApi project in C# with EF6. I use Actions with HttpPost. In one of them I need to retrieve some objects from the DB and send them to a client such as Fiddler in json format. More specifically, I want to get the Title of the Book along with all the Evaluations.



Now I need to create the json below using the code above:




"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":2, "textReview":"Awfull", "stars":1, "book":null, "user":null,
"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":4, "textReview":"Very Good","stars":4, "book":null, "user":null


E.G.: Below you can see the result which I receive from DB, but i cannot make it appear in json format:



https://i.stack.imgur.com/dky9c.png



How can I do this?










share|improve this question
























  • Can you show the Evaluation class?

    – ikerbera
    Nov 15 '18 at 14:58






  • 7





    You really need to start using ANSI-92 style joins. They have been around now for more than 25 years. sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

    – Sean Lange
    Nov 15 '18 at 15:01











  • What version of Entity Framework do you use? I believe the syntax is context.Database.SqlQuery<Evaluation>

    – Hanjun Chen
    Nov 15 '18 at 15:13











  • Hello all and thank u for your reply. I use Entity framework 6.

    – Gizmo
    Nov 15 '18 at 15:28












  • @ikerbera Unfortunately i am not able to edit my Question above, so I will give details for Evaluation class here: [DataContract] public class Evaluations [DataMember(Name ="id")] public int Id get; set; [DataMember(Name = "textReview")] public string TextReview get; set; [DataMember(Name = "stars")] public int Stars get; set; [DataMember(Name = "book")] [Required] public Books Book get; set; [DataMember(Name = "user")] [Required] public Users User get; set;

    – Gizmo
    Nov 15 '18 at 15:52


















0















I have these three tables in SQL Server:



Evaluations (id, textreview, stars, userId, BookId) 
Books (id, title, ..., userId)
Users (id, name, ...)


And I have this code to get data from my tables:



public List<Evaluations> GetAllEvaluationsPerBook()

string sqlCommand = "SELECT DISTINCT b.Title,ev.TextReview,ev.Stars, b.Id " +
"FROM[Services.BookDbContext].[dbo].[Evaluations] as ev, " +
"[Services.BookDbContext].[dbo].Books as b " +
"WHERE b.Id = ev.Book_Id";

using (var context = new BookDbContext())

var evaluation = context.Evaluation.SqlQuery(sqlCommand).ToList();

return evaluation;




Now I'm creating a WebApi project in C# with EF6. I use Actions with HttpPost. In one of them I need to retrieve some objects from the DB and send them to a client such as Fiddler in json format. More specifically, I want to get the Title of the Book along with all the Evaluations.



Now I need to create the json below using the code above:




"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":2, "textReview":"Awfull", "stars":1, "book":null, "user":null,
"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":4, "textReview":"Very Good","stars":4, "book":null, "user":null


E.G.: Below you can see the result which I receive from DB, but i cannot make it appear in json format:



https://i.stack.imgur.com/dky9c.png



How can I do this?










share|improve this question
























  • Can you show the Evaluation class?

    – ikerbera
    Nov 15 '18 at 14:58






  • 7





    You really need to start using ANSI-92 style joins. They have been around now for more than 25 years. sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

    – Sean Lange
    Nov 15 '18 at 15:01











  • What version of Entity Framework do you use? I believe the syntax is context.Database.SqlQuery<Evaluation>

    – Hanjun Chen
    Nov 15 '18 at 15:13











  • Hello all and thank u for your reply. I use Entity framework 6.

    – Gizmo
    Nov 15 '18 at 15:28












  • @ikerbera Unfortunately i am not able to edit my Question above, so I will give details for Evaluation class here: [DataContract] public class Evaluations [DataMember(Name ="id")] public int Id get; set; [DataMember(Name = "textReview")] public string TextReview get; set; [DataMember(Name = "stars")] public int Stars get; set; [DataMember(Name = "book")] [Required] public Books Book get; set; [DataMember(Name = "user")] [Required] public Users User get; set;

    – Gizmo
    Nov 15 '18 at 15:52














0












0








0








I have these three tables in SQL Server:



Evaluations (id, textreview, stars, userId, BookId) 
Books (id, title, ..., userId)
Users (id, name, ...)


And I have this code to get data from my tables:



public List<Evaluations> GetAllEvaluationsPerBook()

string sqlCommand = "SELECT DISTINCT b.Title,ev.TextReview,ev.Stars, b.Id " +
"FROM[Services.BookDbContext].[dbo].[Evaluations] as ev, " +
"[Services.BookDbContext].[dbo].Books as b " +
"WHERE b.Id = ev.Book_Id";

using (var context = new BookDbContext())

var evaluation = context.Evaluation.SqlQuery(sqlCommand).ToList();

return evaluation;




Now I'm creating a WebApi project in C# with EF6. I use Actions with HttpPost. In one of them I need to retrieve some objects from the DB and send them to a client such as Fiddler in json format. More specifically, I want to get the Title of the Book along with all the Evaluations.



Now I need to create the json below using the code above:




"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":2, "textReview":"Awfull", "stars":1, "book":null, "user":null,
"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":4, "textReview":"Very Good","stars":4, "book":null, "user":null


E.G.: Below you can see the result which I receive from DB, but i cannot make it appear in json format:



https://i.stack.imgur.com/dky9c.png



How can I do this?










share|improve this question
















I have these three tables in SQL Server:



Evaluations (id, textreview, stars, userId, BookId) 
Books (id, title, ..., userId)
Users (id, name, ...)


And I have this code to get data from my tables:



public List<Evaluations> GetAllEvaluationsPerBook()

string sqlCommand = "SELECT DISTINCT b.Title,ev.TextReview,ev.Stars, b.Id " +
"FROM[Services.BookDbContext].[dbo].[Evaluations] as ev, " +
"[Services.BookDbContext].[dbo].Books as b " +
"WHERE b.Id = ev.Book_Id";

using (var context = new BookDbContext())

var evaluation = context.Evaluation.SqlQuery(sqlCommand).ToList();

return evaluation;




Now I'm creating a WebApi project in C# with EF6. I use Actions with HttpPost. In one of them I need to retrieve some objects from the DB and send them to a client such as Fiddler in json format. More specifically, I want to get the Title of the Book along with all the Evaluations.



Now I need to create the json below using the code above:




"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":2, "textReview":"Awfull", "stars":1, "book":null, "user":null,
"id":1, "textReview":"Good", "stars":3, "book":null, "user":null,
"id":4, "textReview":"Very Good","stars":4, "book":null, "user":null


E.G.: Below you can see the result which I receive from DB, but i cannot make it appear in json format:



https://i.stack.imgur.com/dky9c.png



How can I do this?







c# sql-server asp.net-web-api entity-framework-6 asp.net-web-api-routing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:03









Joel Coehoorn

313k96497735




313k96497735










asked Nov 15 '18 at 14:55









GizmoGizmo

213




213












  • Can you show the Evaluation class?

    – ikerbera
    Nov 15 '18 at 14:58






  • 7





    You really need to start using ANSI-92 style joins. They have been around now for more than 25 years. sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

    – Sean Lange
    Nov 15 '18 at 15:01











  • What version of Entity Framework do you use? I believe the syntax is context.Database.SqlQuery<Evaluation>

    – Hanjun Chen
    Nov 15 '18 at 15:13











  • Hello all and thank u for your reply. I use Entity framework 6.

    – Gizmo
    Nov 15 '18 at 15:28












  • @ikerbera Unfortunately i am not able to edit my Question above, so I will give details for Evaluation class here: [DataContract] public class Evaluations [DataMember(Name ="id")] public int Id get; set; [DataMember(Name = "textReview")] public string TextReview get; set; [DataMember(Name = "stars")] public int Stars get; set; [DataMember(Name = "book")] [Required] public Books Book get; set; [DataMember(Name = "user")] [Required] public Users User get; set;

    – Gizmo
    Nov 15 '18 at 15:52


















  • Can you show the Evaluation class?

    – ikerbera
    Nov 15 '18 at 14:58






  • 7





    You really need to start using ANSI-92 style joins. They have been around now for more than 25 years. sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

    – Sean Lange
    Nov 15 '18 at 15:01











  • What version of Entity Framework do you use? I believe the syntax is context.Database.SqlQuery<Evaluation>

    – Hanjun Chen
    Nov 15 '18 at 15:13











  • Hello all and thank u for your reply. I use Entity framework 6.

    – Gizmo
    Nov 15 '18 at 15:28












  • @ikerbera Unfortunately i am not able to edit my Question above, so I will give details for Evaluation class here: [DataContract] public class Evaluations [DataMember(Name ="id")] public int Id get; set; [DataMember(Name = "textReview")] public string TextReview get; set; [DataMember(Name = "stars")] public int Stars get; set; [DataMember(Name = "book")] [Required] public Books Book get; set; [DataMember(Name = "user")] [Required] public Users User get; set;

    – Gizmo
    Nov 15 '18 at 15:52

















Can you show the Evaluation class?

– ikerbera
Nov 15 '18 at 14:58





Can you show the Evaluation class?

– ikerbera
Nov 15 '18 at 14:58




7




7





You really need to start using ANSI-92 style joins. They have been around now for more than 25 years. sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

– Sean Lange
Nov 15 '18 at 15:01





You really need to start using ANSI-92 style joins. They have been around now for more than 25 years. sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins

– Sean Lange
Nov 15 '18 at 15:01













What version of Entity Framework do you use? I believe the syntax is context.Database.SqlQuery<Evaluation>

– Hanjun Chen
Nov 15 '18 at 15:13





What version of Entity Framework do you use? I believe the syntax is context.Database.SqlQuery<Evaluation>

– Hanjun Chen
Nov 15 '18 at 15:13













Hello all and thank u for your reply. I use Entity framework 6.

– Gizmo
Nov 15 '18 at 15:28






Hello all and thank u for your reply. I use Entity framework 6.

– Gizmo
Nov 15 '18 at 15:28














@ikerbera Unfortunately i am not able to edit my Question above, so I will give details for Evaluation class here: [DataContract] public class Evaluations [DataMember(Name ="id")] public int Id get; set; [DataMember(Name = "textReview")] public string TextReview get; set; [DataMember(Name = "stars")] public int Stars get; set; [DataMember(Name = "book")] [Required] public Books Book get; set; [DataMember(Name = "user")] [Required] public Users User get; set;

– Gizmo
Nov 15 '18 at 15:52






@ikerbera Unfortunately i am not able to edit my Question above, so I will give details for Evaluation class here: [DataContract] public class Evaluations [DataMember(Name ="id")] public int Id get; set; [DataMember(Name = "textReview")] public string TextReview get; set; [DataMember(Name = "stars")] public int Stars get; set; [DataMember(Name = "book")] [Required] public Books Book get; set; [DataMember(Name = "user")] [Required] public Users User get; set;

– Gizmo
Nov 15 '18 at 15:52













1 Answer
1






active

oldest

votes


















1














I just found the answer which I wanted:



public List<Evaluations> GetAllEvaluationsPerBook()

using (var context = new BookDbContext())

var evaluations = context.Evaluation.ToList();
var books = context.Book.ToList();
var users = context.User.ToList();

return evaluations;




So the code always runs line by line.

The variable evaluations creates a list, filling all its own properties except the Book and the User objects which are remaining null:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":null,
"user":null



After "running" the next line, it fills the list of books. But it fills also the previous list of evaluations with the new list of books:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":
"isbn":1,
"title":"The Tomb",
"author":"H. P. Lovecraft",
"user":null
,
"user":null



Finally it "runs" the line with the users, (retrieving all the users from the DB and creating a list of users) and automatically it fills the previous Lists, so I have all the information retrieved from the DataBase.






share|improve this answer

























  • what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

    – Hanjun Chen
    Nov 16 '18 at 19:18











  • If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

    – Gizmo
    Nov 19 '18 at 15:19












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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322161%2fhow-to-retreive-all-the-properties-of-an-object-which-is-inside-another-object-w%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









1














I just found the answer which I wanted:



public List<Evaluations> GetAllEvaluationsPerBook()

using (var context = new BookDbContext())

var evaluations = context.Evaluation.ToList();
var books = context.Book.ToList();
var users = context.User.ToList();

return evaluations;




So the code always runs line by line.

The variable evaluations creates a list, filling all its own properties except the Book and the User objects which are remaining null:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":null,
"user":null



After "running" the next line, it fills the list of books. But it fills also the previous list of evaluations with the new list of books:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":
"isbn":1,
"title":"The Tomb",
"author":"H. P. Lovecraft",
"user":null
,
"user":null



Finally it "runs" the line with the users, (retrieving all the users from the DB and creating a list of users) and automatically it fills the previous Lists, so I have all the information retrieved from the DataBase.






share|improve this answer

























  • what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

    – Hanjun Chen
    Nov 16 '18 at 19:18











  • If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

    – Gizmo
    Nov 19 '18 at 15:19
















1














I just found the answer which I wanted:



public List<Evaluations> GetAllEvaluationsPerBook()

using (var context = new BookDbContext())

var evaluations = context.Evaluation.ToList();
var books = context.Book.ToList();
var users = context.User.ToList();

return evaluations;




So the code always runs line by line.

The variable evaluations creates a list, filling all its own properties except the Book and the User objects which are remaining null:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":null,
"user":null



After "running" the next line, it fills the list of books. But it fills also the previous list of evaluations with the new list of books:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":
"isbn":1,
"title":"The Tomb",
"author":"H. P. Lovecraft",
"user":null
,
"user":null



Finally it "runs" the line with the users, (retrieving all the users from the DB and creating a list of users) and automatically it fills the previous Lists, so I have all the information retrieved from the DataBase.






share|improve this answer

























  • what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

    – Hanjun Chen
    Nov 16 '18 at 19:18











  • If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

    – Gizmo
    Nov 19 '18 at 15:19














1












1








1







I just found the answer which I wanted:



public List<Evaluations> GetAllEvaluationsPerBook()

using (var context = new BookDbContext())

var evaluations = context.Evaluation.ToList();
var books = context.Book.ToList();
var users = context.User.ToList();

return evaluations;




So the code always runs line by line.

The variable evaluations creates a list, filling all its own properties except the Book and the User objects which are remaining null:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":null,
"user":null



After "running" the next line, it fills the list of books. But it fills also the previous list of evaluations with the new list of books:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":
"isbn":1,
"title":"The Tomb",
"author":"H. P. Lovecraft",
"user":null
,
"user":null



Finally it "runs" the line with the users, (retrieving all the users from the DB and creating a list of users) and automatically it fills the previous Lists, so I have all the information retrieved from the DataBase.






share|improve this answer















I just found the answer which I wanted:



public List<Evaluations> GetAllEvaluationsPerBook()

using (var context = new BookDbContext())

var evaluations = context.Evaluation.ToList();
var books = context.Book.ToList();
var users = context.User.ToList();

return evaluations;




So the code always runs line by line.

The variable evaluations creates a list, filling all its own properties except the Book and the User objects which are remaining null:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":null,
"user":null



After "running" the next line, it fills the list of books. But it fills also the previous list of evaluations with the new list of books:



 
"id":1,
"textReview":"The Book is good.",
"stars":3,
"book":
"isbn":1,
"title":"The Tomb",
"author":"H. P. Lovecraft",
"user":null
,
"user":null



Finally it "runs" the line with the users, (retrieving all the users from the DB and creating a list of users) and automatically it fills the previous Lists, so I have all the information retrieved from the DataBase.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 10:57

























answered Nov 16 '18 at 10:51









GizmoGizmo

213




213












  • what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

    – Hanjun Chen
    Nov 16 '18 at 19:18











  • If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

    – Gizmo
    Nov 19 '18 at 15:19


















  • what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

    – Hanjun Chen
    Nov 16 '18 at 19:18











  • If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

    – Gizmo
    Nov 19 '18 at 15:19

















what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

– Hanjun Chen
Nov 16 '18 at 19:18





what's the purpose of var books = context.Book.ToList() and var users = context.User.ToList(); if they are not being returned?

– Hanjun Chen
Nov 16 '18 at 19:18













If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

– Gizmo
Nov 19 '18 at 15:19






If you do not include them in your code, then the book object which belongs to "evaluations" remains empty. That is what I found out and I posted it in my answer above. ( evaluations consists of some properties. One of them is books object. In order to fill the books property you follow the code above ) :)

– Gizmo
Nov 19 '18 at 15:19




















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322161%2fhow-to-retreive-all-the-properties-of-an-object-which-is-inside-another-object-w%23new-answer', 'question_page');

);

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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo