I have a complex entity CostPageDTO
as shown below:
public class CostPageDTO
{
public string CostPageNumber { get; set; }
public string Description { get; set; }
public char OrderType { get; set; }
public string VendorName { get; set; }
public List<ItemDTO> Items { get; set; }
}
public class ItemDTO
{
public string BrandCode { get; set; }
public string ItemDescription { get; set; }
public string ItemID { get; set; }
}
We need to create a List<CostPageDTO>
from a datatable
. I started as listed below: but I am not sure how to apply the GROUP BY clause here to create a List<ItemDTO>
inside CostPageDTO.
DataTable table = new DataTable();
SqlDataReader reader = command.ExecuteReader();
table.Load(reader);
reader.Close();
List<CostPageDTO> costPages = new List<CostPageDTO>();
Parallel.ForEach(table.AsEnumerable(), (dr) =>
{
costPages.Add(new CostPageDTO()
{
CostPageNumber = dr[0].ToString(),
Description = dr[1].ToString(),
OrderType = Convert.ToChar(dr[2].ToString()),
VendorName = dr[3].ToString()
});
});
- How can we create the required List from DataTable
REFERENCES