Когда я пытаюсь получить список из таблицы, я хочу сделать из него список моделей, как родительский ребенок. Модель и пример кода, которые я пробовал, не дают желаемого результата.
public class EPItemZonePriceGetDto
{
public string ItemCode { get; set; }
public List<EPZonePriceDto> ItemZonePrices { get; set; }
}
public class EPZonePriceDto
{
public int ZoneId { get; set; }
public string Price { get; set; }
}
выборка из списка данных таблицы содержит аналогичные выборочные данные, приведенные ниже:
ZoneId ItemCode Price
1 000100 10
1 000633 10.5
2 000100 11
2 000633 8.8
3 000100 9
3 000633 9.5
4 000100 8.5
4 000633 9.5
IEnumerable<EPItemZonePriceGetDto> data = _entities.ItemZonePrices.Where(x
=>x.ItemCode == itemcode).ToList().Select(s=> new EPItemZonePriceGetDto
{
ItemCode = s.ItemCode,
ItemZonePrices = new List<EPZonePriceDto>()
{
new EPZonePriceDto()
{
Price = s.Price.ToString(),
ZoneId = s.ZoneId
}
}
}
я ожидал, что выход будет как ниже
{ itemZonePrices :
{
itemCode:017273
zonePrice:
[
{ zoneId:1, price:”1.23” },
{ zoneId:2, price:”1.23” }
]
}}
но это дает мне
{itemZonePrices :
{
itemCode:017273
zonePrice:
[
{ zoneId:1, price:”1.23” }
]
},
itemZonePrices :
{
itemCode:017273
zonePrice:
[
{ zoneId:2, price:”1.23” }
]
}
}