如何将SQL语句转换为Linq

时间:2012-02-02 11:18:59

标签: linq-to-sql

我不知道如何将带有join和子查询的Sql语句转换为linq ..以下是我试图翻译的sql语句..请帮助

Select product.Name from product where Product.Id in( select ProductId from 
SaleDetail s join Sale s1 on s.SaleId=s1.Id where s.SaleId in(select Id from Sale  where sale.CustomerId=17264))

1 个答案:

答案 0 :(得分:2)

像这样(db是linq数据上下文):

var result= (
        from p in db.Product
        where
            (
                from s in db.SaleDetail
                join se in db.Sale
                    on s.SaleId equals se.Id
                where
                    (
                        from s2 in db.Sale
                        where s2.CustomerId==17264
                        select s2.Id
                    ).Contains(s.SaleId)
                select s.ProductId
            ).Contains(p.Id)
        select new
        {
            p.Name
        }
    );