从另一个列表中为特定ID创建对象

时间:2017-08-01 01:52:55

标签: c# asp.net

加入2个表后我有以下列表

Id   Name  IsActive FeeName Amount
1    Test       1    Fee1    100
1    Test       1    Fee2    200
1    Test       1    Fee3    300

我有一个叫做产品的课程

 public class Products
{
    public int Id { get; set; }

    public string ProductName { get; set; }

    public bool IsActive { get; set; }

    public List<ProductFee> ProductFunding { get; set; }

}

产品费用类有FeeName和amount属性,

我需要创建一个类似于上面类的对象     1,test,1,ProductFee列表

1 个答案:

答案 0 :(得分:1)

var products = results.GroupBy(r => new { r.Id, r.Name, r.IsActive }).Select(g => new Products(){ Id = g.Key.Id, ProductName = g.Key.Name, IsActive = g.Key.IsActive, ProductFunding = g.Select(p => new ProductFee(){ FeeName = p.FeeName, Amount = p.Amount }).ToList() });