none
LINQ Translation~ RRS feed

  • Question

  • Can anyone please help to translate this SQL query to LINQ syntax for me? I am very new in LINQ.

    select * from Product where ProductID in (select ProductID from ProductDetail where Price > 100)

    Thanks very much

    Tuesday, April 6, 2010 7:17 AM

Answers

  • Short / method syntax, using navigation properties:

    var prods = dc.Products.Where(p => p.ProductDetails.Any(pd => pd.Price > 100));

    Longer / query syntax, not using nav properties:

    var prods = from p in dc.Products
      where (
        from pd in dc.ProductDetails
        where pd.Price > 100
        select pd.ProductID
        ).Contains(p.ProductID)
      select p;


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)

    Tuesday, April 6, 2010 7:32 AM
    Answerer

All replies

  • Short / method syntax, using navigation properties:

    var prods = dc.Products.Where(p => p.ProductDetails.Any(pd => pd.Price > 100));

    Longer / query syntax, not using nav properties:

    var prods = from p in dc.Products
      where (
        from pd in dc.ProductDetails
        where pd.Price > 100
        select pd.ProductID
        ).Contains(p.ProductID)
      select p;


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)

    Tuesday, April 6, 2010 7:32 AM
    Answerer
  • Thanks for the quick response~
    Tuesday, April 6, 2010 8:14 AM