Left outer join with agregate RRS feed

  • Question

  • I have the following sql:

    select dbo.products.*, dbo.ProductImages.* from dbo.products left outer join

    (select min(Id) m, ProductId from dbo.ProductImages group by productId) b on dbo.products.Id = b.ProductId left outer join

    dbo.ProductImages on m = dbo.ProductImages.Id

    The idea is return a list of the first image (or null if no images) for each product + the product.

    Is it possible to create such expression in linq to sql?


    Wednesday, February 1, 2012 4:17 PM


  • Hi gooky,

    Welcome to MSDN Forum.

    Please try the code below.

    var query = (from p in context.Products join i in context.ProductImages on p.ID equals i.PID select new { product = p, image = p.Images.FirstOrDefault() }).Distinct();

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, February 3, 2012 3:23 AM