简体   繁体   English

如何将此linq命令写入等效的删除SQL查询?

[英]How do I write this linq command to an equivalent delete SQL query?

I haven't written SQL in many years and now I'm working on a little more "performance hungry" site. 多年来我没有编写过SQL,现在我正在开发一个“性能饥渴”的网站。 Could someone please help me convert this to a delete SQL command? 有人可以帮我转换为删除SQL命令吗?

I have a bunch of these so helping me with this one would sort out a lot for me, thanks! 我有很多这样的帮助我,这个对我来说很重要,谢谢!

I got this far: 我到目前为止:

Delete FROM [DbName].[dbo].[RatingListTriangleModels] 
WHERE  ...

Linq select command: Linq选择命令:

var allRatingListObjects = from row in ctx.RatingListTriangle
                           where
                              (row.To1Id == myToid && row.To2Id == theirTradeObjectid)
                               || (row.To2Id == myToid && row.To3Id == theirTradeObjectid)
                               || (row.To3Id == myToid && row.To1Id == theirTradeObjectid)
                           select row;

Update: Here is the working solution I went for: 更新:这是我的工作解决方案:

using (SqlCommand command = new SqlCommand
                ("delete TBL from RatingListTriangleModels TBL where ( TBL.To1Id = @MY_ID and TBL.To2Id = @OTHER_ID ) or ( TBL.To2Id = @My_ID and TBL.To3Id = @OTHER_ID ) or ( TBL.To3Id = @My_ID and TBL.To1Id = @OTHER_ID )", cn))
            {
                // Add new SqlParameter to the command.
                command.Parameters.Add(new SqlParameter("MY_ID", myToid));
                command.Parameters.Add(new SqlParameter("OTHER_ID", theirTradeObjectid));
                var no = command.ExecuteNonQuery();
            }

This should delete the records you specify in your linq-statement but you need to replace @YOUR_ID and @OTHER_ID with the corresponding ids you have. 这应该删除您在linq语句中指定的记录,但是您需要将@YOUR_ID@OTHER_ID替换为您拥有的相应ID。

You can do this with SqlParameter . 您可以使用SqlParameter执行此操作。 Example

delete from [DbName].[dbo].[RatingListTriangleModels] TBL
where ( TBL.To1Id = @YOUR_ID and TBL.To2Id = @OTHER_ID )
   or ( TBL.To2Id = @YOUR_ID and TBL.To3Id = @OTHER_ID )
   or ( TBL.To3Id = @YOUR_ID and TBL.To1Id = @OTHER_ID )  

If you want to delete the rows that that linq selects with TSQL you could do 如果要删除linq使用TSQL选择的行,则可以执行此操作

DECLARE @p0 int;
DECLARE @p1 int;
SET @p0 = <some integer>;
SET @p1 = <some other integer>;


DELETE
            [DbName].[dbo].[RatingListTriangleModels]
    WHERE
            ([To1Id] = @p0 AND [To2Id] = @p1)
        OR  
            ([To2Id] = @p0 AND [To3Id] = @p1)
        OR
            ([To3Id] = @p0 AND [To1Id] = @p1);

Of course you'll probably want to pass the statement to ExecuteCommand on your context and pass the ids as a parameters, then .Net will wrap this in a call to sp_executeSql for you. 当然,您可能希望将语句传递给上下文中的ExecuteCommand并将id作为参数传递,然后.Net将为您调用sp_executeSql This will protect from injection attacks, give you the benefits of query plan reuse and the flexibility of direct TSQL manipulation. 这将防止注入攻击,为您提供查询计划重用的好处和直接TSQL操作的灵活性。

I would caveat my answer, a simple operation like this would problably be performed equivalently in linq without any of this additional complication. 我会告诫我的答案,像这样的简单操作可能会在linq中等效执行,而不会出现任何额外的复杂情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM