I haven't written SQL in many years and now I'm working on a little more "performance hungry" site. Could someone please help me convert this to a delete SQL command?
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:
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.
You can do this with 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
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. This will protect from injection attacks, give you the benefits of query plan reuse and the flexibility of direct TSQL manipulation.
I would caveat my answer, a simple operation like this would problably be performed equivalently in linq without any of this additional complication.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.