Me gustaría saber si hay una manera más fácil de insertar un registro si aún no existe en una tabla. Todavía estoy tratando de desarrollar mis habilidades de LINQ to SQL.LINQ to SQL insert-if-inexistente
Esto es lo que tengo, pero parece que debería haber una manera más fácil.
public static TEntity InsertIfNotExists<TEntity>
(
DataContext db,
Table<TEntity> table,
Func<TEntity,bool> where,
TEntity record
)
where TEntity : class
{
TEntity existing = table.SingleOrDefault<TEntity>(where);
if (existing != null)
{
return existing;
}
else
{
table.InsertOnSubmit(record);
// Can't use table.Context.SubmitChanges()
// 'cause it's read-only
db.SubmitChanges();
}
return record;
}
¿Qué es e => e.BooleanProperty? Las entidades no tienen un miembro BooleanProperty. Nunca he visto esto antes ... – core
Hmm, notarás que en mi código, dije que table.Context.SubmitChanges() no funcionó porque es solo de get. Aparentemente cometí un error. – core
e => e.BooleanProperty es solo un ejemplo. Puede representar cualquier expresión que devuelva un valor booleano. –