.Net Core 2.1 return list with dependent type by id











up vote
0
down vote

favorite












For example, I have orders model:



int Id
string Name
User user


How to return something like that in response when getting orders, when orders table has user id column:



{
id: 1,
name: "Order 1",
user: "Some username"
}


I know it can look easy question, but I'm new to C# and .NET and don't exactly know the terminology and for what tutorial should I look for.










share|improve this question


















  • 1




    The latter is called json.
    – Sinatr
    Nov 7 at 13:47










  • go through these documentations docs.microsoft.com/en-us/ef/core
    – Neville Nazerane
    Nov 7 at 13:48










  • Yes, im asking how to merge two tables in response from .net side. I know i want return json, i already returning orders, but wanted to return advanced structure, mapped from 2 tables.
    – user3462947
    Nov 7 at 14:05















up vote
0
down vote

favorite












For example, I have orders model:



int Id
string Name
User user


How to return something like that in response when getting orders, when orders table has user id column:



{
id: 1,
name: "Order 1",
user: "Some username"
}


I know it can look easy question, but I'm new to C# and .NET and don't exactly know the terminology and for what tutorial should I look for.










share|improve this question


















  • 1




    The latter is called json.
    – Sinatr
    Nov 7 at 13:47










  • go through these documentations docs.microsoft.com/en-us/ef/core
    – Neville Nazerane
    Nov 7 at 13:48










  • Yes, im asking how to merge two tables in response from .net side. I know i want return json, i already returning orders, but wanted to return advanced structure, mapped from 2 tables.
    – user3462947
    Nov 7 at 14:05













up vote
0
down vote

favorite









up vote
0
down vote

favorite











For example, I have orders model:



int Id
string Name
User user


How to return something like that in response when getting orders, when orders table has user id column:



{
id: 1,
name: "Order 1",
user: "Some username"
}


I know it can look easy question, but I'm new to C# and .NET and don't exactly know the terminology and for what tutorial should I look for.










share|improve this question













For example, I have orders model:



int Id
string Name
User user


How to return something like that in response when getting orders, when orders table has user id column:



{
id: 1,
name: "Order 1",
user: "Some username"
}


I know it can look easy question, but I'm new to C# and .NET and don't exactly know the terminology and for what tutorial should I look for.







c# .net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 13:46









user3462947

647




647








  • 1




    The latter is called json.
    – Sinatr
    Nov 7 at 13:47










  • go through these documentations docs.microsoft.com/en-us/ef/core
    – Neville Nazerane
    Nov 7 at 13:48










  • Yes, im asking how to merge two tables in response from .net side. I know i want return json, i already returning orders, but wanted to return advanced structure, mapped from 2 tables.
    – user3462947
    Nov 7 at 14:05














  • 1




    The latter is called json.
    – Sinatr
    Nov 7 at 13:47










  • go through these documentations docs.microsoft.com/en-us/ef/core
    – Neville Nazerane
    Nov 7 at 13:48










  • Yes, im asking how to merge two tables in response from .net side. I know i want return json, i already returning orders, but wanted to return advanced structure, mapped from 2 tables.
    – user3462947
    Nov 7 at 14:05








1




1




The latter is called json.
– Sinatr
Nov 7 at 13:47




The latter is called json.
– Sinatr
Nov 7 at 13:47












go through these documentations docs.microsoft.com/en-us/ef/core
– Neville Nazerane
Nov 7 at 13:48




go through these documentations docs.microsoft.com/en-us/ef/core
– Neville Nazerane
Nov 7 at 13:48












Yes, im asking how to merge two tables in response from .net side. I know i want return json, i already returning orders, but wanted to return advanced structure, mapped from 2 tables.
– user3462947
Nov 7 at 14:05




Yes, im asking how to merge two tables in response from .net side. I know i want return json, i already returning orders, but wanted to return advanced structure, mapped from 2 tables.
– user3462947
Nov 7 at 14:05












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Basically what you need is to return a Data Transfer Object (DTO).
Create another class, say OrderDTO.



public class OrderDto
{
public int Id { get; set; }
public string Name { get; set; }
public string User { get; set; }
}


Then map your order object(s) to OrderDto before returning them as your response.



 var oderDto = new OrderDto { 
Id = orderModel.Id,
Name = orderModel,
User = oder.User.Username // or some other property you want
};


Automapper would be a very good library to use for the mapping part.



Your actions will then look something like this



 public ActionResult GetStuff()
{
var orderModel = ....; // do your thing to get your order
var responseObj = Mapper.Map<OrderDto>(orderModel);
return StatusCode(200, responseObj);
}


The framework will then do the conversion from the DTO class to Json for you.






share|improve this answer























  • Awesome response. Thanks.
    – user3462947
    Nov 7 at 14: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',
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%2f53190728%2fnet-core-2-1-return-list-with-dependent-type-by-id%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








up vote
2
down vote



accepted










Basically what you need is to return a Data Transfer Object (DTO).
Create another class, say OrderDTO.



public class OrderDto
{
public int Id { get; set; }
public string Name { get; set; }
public string User { get; set; }
}


Then map your order object(s) to OrderDto before returning them as your response.



 var oderDto = new OrderDto { 
Id = orderModel.Id,
Name = orderModel,
User = oder.User.Username // or some other property you want
};


Automapper would be a very good library to use for the mapping part.



Your actions will then look something like this



 public ActionResult GetStuff()
{
var orderModel = ....; // do your thing to get your order
var responseObj = Mapper.Map<OrderDto>(orderModel);
return StatusCode(200, responseObj);
}


The framework will then do the conversion from the DTO class to Json for you.






share|improve this answer























  • Awesome response. Thanks.
    – user3462947
    Nov 7 at 14:19















up vote
2
down vote



accepted










Basically what you need is to return a Data Transfer Object (DTO).
Create another class, say OrderDTO.



public class OrderDto
{
public int Id { get; set; }
public string Name { get; set; }
public string User { get; set; }
}


Then map your order object(s) to OrderDto before returning them as your response.



 var oderDto = new OrderDto { 
Id = orderModel.Id,
Name = orderModel,
User = oder.User.Username // or some other property you want
};


Automapper would be a very good library to use for the mapping part.



Your actions will then look something like this



 public ActionResult GetStuff()
{
var orderModel = ....; // do your thing to get your order
var responseObj = Mapper.Map<OrderDto>(orderModel);
return StatusCode(200, responseObj);
}


The framework will then do the conversion from the DTO class to Json for you.






share|improve this answer























  • Awesome response. Thanks.
    – user3462947
    Nov 7 at 14:19













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Basically what you need is to return a Data Transfer Object (DTO).
Create another class, say OrderDTO.



public class OrderDto
{
public int Id { get; set; }
public string Name { get; set; }
public string User { get; set; }
}


Then map your order object(s) to OrderDto before returning them as your response.



 var oderDto = new OrderDto { 
Id = orderModel.Id,
Name = orderModel,
User = oder.User.Username // or some other property you want
};


Automapper would be a very good library to use for the mapping part.



Your actions will then look something like this



 public ActionResult GetStuff()
{
var orderModel = ....; // do your thing to get your order
var responseObj = Mapper.Map<OrderDto>(orderModel);
return StatusCode(200, responseObj);
}


The framework will then do the conversion from the DTO class to Json for you.






share|improve this answer














Basically what you need is to return a Data Transfer Object (DTO).
Create another class, say OrderDTO.



public class OrderDto
{
public int Id { get; set; }
public string Name { get; set; }
public string User { get; set; }
}


Then map your order object(s) to OrderDto before returning them as your response.



 var oderDto = new OrderDto { 
Id = orderModel.Id,
Name = orderModel,
User = oder.User.Username // or some other property you want
};


Automapper would be a very good library to use for the mapping part.



Your actions will then look something like this



 public ActionResult GetStuff()
{
var orderModel = ....; // do your thing to get your order
var responseObj = Mapper.Map<OrderDto>(orderModel);
return StatusCode(200, responseObj);
}


The framework will then do the conversion from the DTO class to Json for you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 14:30









Rui Jarimba

7,00962958




7,00962958










answered Nov 7 at 14:15









gerryc.inc

47424




47424












  • Awesome response. Thanks.
    – user3462947
    Nov 7 at 14:19


















  • Awesome response. Thanks.
    – user3462947
    Nov 7 at 14:19
















Awesome response. Thanks.
– user3462947
Nov 7 at 14:19




Awesome response. Thanks.
– user3462947
Nov 7 at 14:19


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190728%2fnet-core-2-1-return-list-with-dependent-type-by-id%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()