I have been writing same code for insert, update, delete with LINQ over and over again. I want to have some sort of generic function for Insert, Update, Delete operation. I read a post here like the following :
public static void Insert<T>(T entity) where T : class
{
using (OrcasDB database = new OrcasDB())
{
database.GetTable<T>().Add(entity);
database.SubmitChanges();
}
}
public static void Delete<T>(Expression<Func<T, bool>> predicate)
where T : class
{
using (OrcasDB database = new OrcasDB())
{
T instance = (T) database.GetTable<T>().Where<T>(predicate).Single();
database.GetTable<T>().Remove(instance);
database.SubmitChanges();
}
}
How to Use
// insert
Employee will = new Employee
{
Username = "will.asrari",
EmailAddress = "me@willasrari.com",
CanCode = true
};
LinqHelper.Insert<Employee>(will);
// delete
LinqHelper.Delete(emp => emp.EmployeeId.Equals(3));
Yes, I would like to write something like in VB.NET. Is the code above good to follow? Can anyone show me any LINQ to SQL generic class for Insert, Delete, Update written in VB.NET?
Thank you.
Best Answer
FYI, I managed to write a simple class to do the generic CUD operantion for LINQ to SQL.
'Class GenericCUD.vb
Imports System.Linq.Expressions Imports System.Data.Linq
Public Class GenericCUD
End Class
How to use the class :
I read a lot of post on many blogs. Here are a few that really helped me to make the GenericCUD work:
So, What do you think about the GernericCUD class above? Please give me some comment because I want to improve it. Thank you.