Last active
January 4, 2016 00:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good my friend!!!!!