I use a IActionResult to get a List. If I have a list, the result is ok. If I use a List, the elements of the result are empty.
If I use a generic List, it works:
public IActionResult GetData()
{
var erg1 = _database.table.Select(x => new { x.Id, x.firstname, x.lastname });
return Ok(erg1);
}
I get a List with all Elements:
[
{
"id": "732fdbd7-c878-45e8-8c43-5f795a697f6d",
"firstname": "Uwe",
"lastname": "Gabbert"
},
{
"id": "5288f9ea-25a2-4ffc-a711-7c0b2cf49c38",
"firstname": "User",
"lastname": "Test"
}
]
But, If I use a separate class for the elements, I get a empty List:
public IActionResult GetData()
{
var erg2 = _database.table.Select(x => new User( x.Id, x.firstname, x.lastname ));
return Ok(erg2);
}
In erg2 are 2 Items of User. But the return of getData is:
[
{},
{}
]
here the User-class:
public class User
{
public string Id = "";
public string Firstname = "";
public string Lastname = "";
public User(string id, string firstname, string lastname)
{
Id = id;
Firstname = firstname;
Lastname = lastname;
}
}