我熟悉简单GROUP BY
的操作LINQ
。但我有一个复杂的场景。我需要使用 group by将CostPageRow
记录转换为域对象。CostPageSelection
我们如何从列表中获取域对象?
注意:我上面写的 group by 没有给出 List。我不知道怎么写。CostPageSelection 里面有一个列表。
List<CostPageRow> costPageRows = searchDAL
.GetAllCostPages(contextObject, searchCriteria);
var orderGroups = costPageRows
.GroupBy(x => new {
x.CostPage,
x.Description,
x.BillTypeDirect,
x.BillTypeWarehouse,
x.OrderType,
x.Vendor,
x.VendorID
})
.Select(y => new CostPageParent {
CostPage = y.First().CostPage
})
.ToList();
存储过程的结果
public class CostPageRow
{
//CostPage Parent
public string CostPage { get; set; }
public string Description { get; set; }
public string Vendor { get; set; }
public string VendorID { get; set; }
public string BillTypeDirect { get; set; }
public string BillTypeWarehouse { get; set; }
public string OrderType { get; set; }
//Item Chilld
public string ItemID { get; set; }
public string ItemDescription { get; set; }
public string BrandCode { get; set; }
public string PackSize { get; set; }
}
领域模型
public class CostPageSelection
{
public CostPageParent CostPageParent { get; set; }
public List<ItemChild> ChildItems { get; set; }
}
//CostPageParent
public class CostPageParent
{
public int? SelectedCostPageID { get; set; }
public string CostPage { get; set; }
public string Description { get; set; }
public string Vendor { get; set; }
public string VendorID { get; set; }
public string BillTypeDirect { get; set; }
public string BillTypeWarehouse { get; set; }
public string OrderType { get; set; }
}
//ItemChild
public class ItemChild
{
public int? SelectedItemID { get; set; }
public string ItemID { get; set; }
public string ItemDescription { get; set; }
public string BrandCode { get; set; }
public string PackSize { get; set; }
}