Click here to Skip to main content
15,896,111 members
Articles / All Topics

Linq2Sql Delete by Key with Object Tracking

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Jul 2013CPOL 5.5K   1  
Linq2Sql delete by key with object tracking

Introduction

In Linq2Sql, if you want to delete object, it has to be loaded (or attached) first which results in not very clean syntax. There are some solutions on the net that build "DELETE ... WHERE ..." statement. This approach has one problem: it breaks Linq2Sql object tracking. So I created my own implementation that can be invoked like this:

C#
dbContext.Consumers.DeleteByKey(key, null);

If you want to do some custom processing when object is deleted (for example, delete related objects), you can do it like this:

C#
dbContext.Consumers.DeleteByKey(key,
    c =>
    {
   var related = dbContext.ConsumerRelatedObject.Where(x => x.ConsumerId == key);

    dbContext.ConsumerRelatedObject.DeleteAllOnSubmit(related);
    });

And the function itself:

C#
public static class TableExtensions
    {
        public static void DeleteByKey<TEntity, TPK>(this Table<TEntity> table, 
TPK key, Action<TEntity> onDelete) where TEntity : class 
        {
            var pkProp = (from p in typeof(TEntity).GetProperties()
                          from c in p.GetCustomAttributes(typeof(ColumnAttribute), 
false).OfType<ColumnAttribute>()
                          where c.IsPrimaryKey
                          select p).Single();

            var param = Expression.Parameter(typeof(TEntity), "param");
            var propExpr = Expression.Property(param, pkProp);
            var valExpr = Expression.Constant(key, pkProp.PropertyType);
            var l = Expression.Lambda<Func<TEntity, bool>>
(Expression.Equal(propExpr, valExpr), param);

            var obj = table.Where(l).SingleOrDefault();

            if (obj != null)
            {
                if (onDelete != null) onDelete(obj);
                table.DeleteOnSubmit(obj);
            }
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --