I'm trying to perform a DELETE using LINQ that will generate a single query.
Here's how I'm doing it:
// NorthwintEntities is an ADO.NET Entitity Data Model
var northwindEntities = new NorthwindEntities();
northwindEntities.Order_Details.Delete(o => o.Order_ID == 11076);
Here's my Extension:
public static class EntityExtensions
{
private static Regex rxTableName = new Regex(@"^FROM\s+(?<table>\[[^\]]*\](\.\[[^\]]*\]){0,2})\s+AS\s+(?<alias>\[[^\]]*\])", RegexOptions.Multiline);
public static void Delete<T>(this ObjectSet<T> entity, Expression<Func<T, bool>> expression) where T : EntityObject
{
var selectQuery = entity.Where(expression).Select(x => 1);
string selectQueryString = ((ObjectQuery)selectQuery).ToTraceString();
string deleteQueryString = ConvertSqlSelectToDelete(selectQueryString);
entity.Context.ExecuteStoreCommand(deleteQueryString);
}
private static string ConvertSqlSelectToDelete(string selectQuery)
{
if (selectQuery.IndexOf(" JOIN ") > -1)
{
throw new Exception("Query with JOIN is not supported: " + selectQuery);
}
Match match = rxTableName.Match(selectQuery);
if (!match.Success)
{
throw new Exception("Unable to convert SELECT: " + selectQuery);
}
string deleteQuery = "DELETE \r\n" + selectQuery.Substring(match.Index);
deleteQuery = deleteQuery.Replace(match.Groups["alias"].Value + ".", "");
deleteQuery = deleteQuery.Replace("AS " + match.Groups["alias"].Value, "");
return deleteQuery;
}
}
This works, but I have a few comments.
So my questions is: Is there an easier way to do this? If so, what is it?
Any help at all will be appreciated.
RemoveRange. The RemoveRange method is used for deleting multiple objects from the database in one method call. The following code deletes a large number of records from the database using RemoveRange.
Proper way to delete record in LINQ to Entities My delete function worked when I did the following:... using (var ctx = new MyEntity()) { var x = (from y in ctx. Employees orderby y. EmployeeId descending select y).
Delete a Record In Connected Scenario, you can use the Remove or RemoveRange method to mark the record as Deleted . In Disconnected Scenario, you can attach it to the context and set its state as Deleted . Calling SaveChanges will send the delete query to the database.
Install Entity Framework Extended Library (PM> Install-Package EntityFramework.Extended)
Import EntityFramework.Extensions in your code
Delete records specified by inner query
context.Orders.Where(o=>o.User_ID == 1).Delete();
Deletes all records inside Orders with userID = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With