Skip to content

Instantly share code, notes, and snippets.

@luidones
Last active January 4, 2016 00:59
Show Gist options
  • Save luidones/8545630 to your computer and use it in GitHub Desktop.
Save luidones/8545630 to your computer and use it in GitHub Desktop.
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;
else
result = source.SingleOrDefault(comparer);
return result;
}
@eduardosilva
Copy link

Very good my friend!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment