如何将我的删除SQL查询更改为LINQ

时间:2012-06-30 18:10:22

标签: linq

我在LINQ字段中有点新。我有一个像这样的删除查询:

DELETE FROM Table1 WHERE FK IN (SELECT ID FROM Table2 WHERE UserId = @UId)

如何将其更改为LINQ查询?

1 个答案:

答案 0 :(得分:0)

这应该给你一个想法

var itemQuery = from cartItems in db.SalesOrderDetails
              where cartItems.SalesOrderID == 75144
              select cartItems.ProductID;


var myProducts = from p in db.Products
                where itemQuery.Contains(p.ProductID)
                select p;

foreach (var detail in myProducts )
{
    db.myProducts.DeleteOnSubmit(detail);
}

try
{
    db.SubmitChanges();
}
catch (Exception e)
{
    Console.WriteLine(e);
    // Provide for exceptions.
}