I am currently fetching a list of object from the database and would like to return it in a certain way from my ASP.Net Web API with Automapper. My Object currently looks like this after fetching it from the database:
[
{
"id": 1,
"dropdown": "Country",
"value": "Germany"
},
{
"id": 2,
"dropdown": "Country",
"value": "United States"
},
{
"id": 5,
"dropdown": "Type",
"value": "Lead"
},
{
"id": 6,
"dropdown": "Type",
"value": "Account"
},
{
"id": 7,
"dropdown": "Type",
"value": "Reseller"
},
{
"id": 8,
"dropdown": "Type",
"value": "Distributor"
}
]
But I would like it to look like this:
[{
"Countries": [{
"id": 1,
"value": "Germany"
},
{
"id": 2,
"value": "United States"
}
]
},
{
"Type": [{
"id": 5,
"value": "Lead"
},
{
"id": 6,
"value": "Account"
}
]
}
]
Currently my CreateMap looks like this
CreateMap<DropdownValue, DropdownValueListDto>();
And my DropdownValueListDto like this
public class DropdownValueListDto
{
public int Id { get; set; }
public string Dropdown { get; set; }
public string Value { get; set; }
}
My LINQ Operation looks like this:
public async Task<IEnumerable<DropdownValue>> GetDropdownValues(string[] dropdowns)
{
var dropdownValues = _context.DropdownValues.OrderBy(x => x.Id).ThenBy(x => x.Dropdown).AsQueryable();
if (dropdowns.Length != 0 || dropdowns != null)
{
dropdownValues = dropdownValues.Where(x => dropdowns.Contains(x.Dropdown));
}
var dropdownValuesToReturn = await dropdownValues.ToListAsync();
return dropdownValuesToReturn;
}
It would be great if someone could help me achieve this.
Thanks in advance
DropDownValueListDtoobject... Is the JSON stuff more like you expect to see?