Update Method for LINQ
8/14/2007 12:37:38 PM
Again, quick little piece of code I have been working on for updating data using LINQ. This method needs to be cleaned up so as to not allow setting of the Primary Key value (or DbGenerated field). That will come at a later time. This is simply shooting from the hip.
public
static void Update<T>(Expression<Func<T, bool>> predicate, T entity)
where T : class
{
if (!EntityExists<T>(predicate))
using (TDatabase database = new TDatabase())
{
T t = (T) database.GetTable<T>().Where<T>(predicate).Single();
foreach (PropertyInfo p in t.GetType().GetProperties())
p.SetValue(t, p.GetValue(entity, null), null);
database.SubmitChanges();
}
}
Read this entry for reference.
C#,
Code,
LINQ
