.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.
c# .net
add a comment |
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.
c# .net
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
add a comment |
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.
c# .net
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
c# .net
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
add a comment |
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
add a comment |
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.
Awesome response. Thanks.
– user3462947
Nov 7 at 14:19
add a comment |
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.
Awesome response. Thanks.
– user3462947
Nov 7 at 14:19
add a comment |
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.
Awesome response. Thanks.
– user3462947
Nov 7 at 14:19
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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%2f53190728%2fnet-core-2-1-return-list-with-dependent-type-by-id%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
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