将项添加到集合中的集合

时间:2011-05-24 06:47:31

标签: c#-4.0

这是我的对象结构,

Group has         
     List of Offers
Offer has 
     GroupId
     List of Products

如何根据组ID

将产品添加到现有产品列表中

此代码正在添加产品,但会覆盖现有产品

Group.OffersList.Where(x => x.GroupId == "1")
                .SelectMany(x => x.ProductList)
                .ToList().Add(Product);

1 个答案:

答案 0 :(得分:0)

Group.OffersList.Where(x => x.GroupId == "1")
                 .ToList()
                 .ForEach(x => x.Add(Product));