Skip to content

Instantly share code, notes, and snippets.

View luidones's full-sized avatar

Luiggi Anguiano luidones

  • São Paulo - Brasil
View GitHub Profile
@luidones
luidones / AddedOrSingleOrDefault.cs
Last active January 4, 2016 00:59
Extension method to check if the condition is satisfied first in the added but not commited items then in the database.
internal static T AddedOrSingleOrDefault<T>(this IDbSet<T> source, DbContext context, Func<T, bool> comparer)
where T : class
{
T result = null;
var entry = context.ChangeTracker.Entries<T>()
.SingleOrDefault(e => e.State == EntityState.Added && comparer(e.Entity));
if (entry != null)
result = entry.Entity;