Question

I have a linq sql query:

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
    };

I'm trying to get the data from Product_Styles to tag along, but it's not working...

The above works, but not when i uncomment the bits above....

Was it helpful?

Solution

Include style in your group, then reference orderItem and style in your select:

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()
      };

You may have to adjust this to suit your needs, and it may not compile exactly as-is since I don't have an environment set up to test it in, but it should give you a general idea.

OTHER TIPS

according to @GrantWinney post, you need to select Name, and ImagePath like this:

Name = grp.FirstOrDefault().style.Name,
ImagePath = grp.FirstOrDefault().style.External_Image_Path
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top