Linq 中使用Join into Aggregate,Left out join,

问题:

    Products={{ID=1,Name=ProductName_1},{ID=2,Name=ProductName_2},......,{ID=n,Name=ProductName_n}},

    Orders ={{ID=1,ProductID=ProductID_1,CustomerName=CustomerName_1},......,{ID=m,ProductID=ProductID_m,CustomerName=CustomerName_m}}

    怎样产生

    ProductSummary={{ProductName=ProductName1 , Customs="CustomerName_i1,....,CustomerName_is1,"},.....,{ProductName=ProductNamen , Customs="CustomerName_in,....,CustomerName_isn,"}}

   其中CustomerName_in,....,CustomerName_isn,是所有购买了Product n的客户名字。 如果一个产品没有人订 CustomerName="无人问津"

解决办法:

   var query = from p in Products

                     join o in Orders on p.ID=o.ProductID into og

                     select new { ProductName = p.Name, Customers=og.DefaultIfEmpty(new Order { CustomerName="无人问津"}).Aggregate("",(x,y)=>x+y.CustomerName+",")};

推广:可以使用Aggregate来计算更多的统计信息,如总销量....

原文地址:https://www.cnblogs.com/mjgb/p/2113840.html