Complex GROUP BY on DataTable

前端 未结 1 1732
萌比男神i
萌比男神i 2021-01-16 14:50

I have a complex entity CostPageDTOas shown below:

public class CostPageDTO
{
    public string CostPageNumber { get; set; }
    public string D         


        
相关标签:
1条回答
  • 2021-01-16 15:37

    I'm assuming your database query returns a join between CostPage and Item. If that's the case, first you have to group your rows to get the values for CostPage, after that project to your DTO type. I really doubt that you'll see much benefit in parallelizing the code.

    Your code should look roughly like this:

    costPages = table.AsEnumerable().GroupBy(dr=> new 
                {
                    CostPageNumber  = dr[0].ToString(),
                    Description = dr[1].ToString(),
                    OrderType = Convert.ToChar(dr[2].ToString()),
                    VendorName = dr[3].ToString()
                })
                .Select(x => new CostPageDTO(){
                    CostPageNumber = x.Key.CostPageNumber,
                    Description = x.Key.Description,
                    OrderType = x.Key.OrderType,
                    VendorName = x.Key.VendorName,
                    Items = x.Select(dr=> new ItemDTO{
                        //ItemDTO mapping goes here
                        ItemID=dr[Constants.SearchPage.ITMID].ToString()
                    }).ToList()
                }).ToList();
    
    0 讨论(0)
提交回复
热议问题