带有join的ling sql组

时间:2014-03-18 17:28:01

标签: c# linq linq-to-sql

我有一个linq sql查询:

var orderitems =
    from orderItem in Order_ProductItems
    //join style in Products_Styles on orderItem.Style equals style.Index
    where orderItem.SalesOrderID == 123
    group orderItem by orderItem.FrameNo into grp
    select new
    {
        FrameNo = grp.Key,
        Count = grp.Select(x => x.FrameNo).Count(),
        TotalCost = grp.Sum(x=>x.CostPrice),
        OverAllWidth = grp.Single(x=>x.HardwareType==3).OverallHeight,
        //Name = style.Name, 
        //ImagePath = style.External_Image_Path
    };

我正在尝试将Product_Styles中的数据添加到标记中,但它无效...

以上是有效的,但是当我取消注释上面的内容时......

2 个答案:

答案 0 :(得分:0)

在您的论坛中加入style,然后在orderItem中引用styleselect

var orderitems =
    (from orderItem in Order_ProductItems
     join style in Products_Styles on orderItem.Style equals style.Index
     where orderItem.SalesOrderID == 123
     group new { orderItem, style } by orderItem.FrameNo into grp
     select new
     {
         FrameNo = grp.Key,
         Count = grp.Select(x => x.orderItem.FrameNo).Count(),
         TotalCost = grp.Sum(x => x.orderItem.CostPrice),
         OverAllWidth = grp.Single(x => x.orderItem.HardwareType == 3).OverallHeight,
         Name = grp.Select(x => x.style.Name).First(),
         ImagePath = grp.Select(x => x.style.External_Image_Path).First()
      };

您可能需要根据自己的需要进行调整,并且可能无法完全按原样进行编译,因为我没有设置测试环境,但它应该给您一个大致的想法。< / p>

答案 1 :(得分:0)

根据@GrantWinney帖子,您需要选择NameImagePath,如下所示:

Name = grp.FirstOrDefault().style.Name,
ImagePath = grp.FirstOrDefault().style.External_Image_Path