Convert SQL update query to c# LINQ code

481 Views Asked by At

I want to convert this SQL query to C# code:

UPDATE Layouts 
SET LayoutDetails.modify('delete /ColumnLayout/LayoutColumns/Column[@PropertyId = sql:variable("@propId")]') 
WHERE LayoutId = 236

This query deletes a node in a XML column in a SQL Server table. I want to perform a similar operation in C# using LINQ.

2

There are 2 best solutions below

2
On

LINQ does not have anything like this available, because LINQ doesn't interact with a database in that way. I'm not sure what you are using to connect to your database, but there should be something in there that would allow you to do this.

As an example, Entity Framework has SaveChanges(), which will push any changes made to objects as an UPDATE sql command to the database you're connected to. Other types of ORMs should have something similar.

1
On

If it is possible to read that XML column, then you can update that column and then use SaveChanges() method. like

var details = (from lay in context.Layouts where lay.id = 236 select lay).firstOrdefalut();

details.ColumnLayout.LayoutColumns.PropertyId = yourId;

saveChange();

Check it will be helpful