-
-
Save mshwf/af017b50ceb2994d953762ef2b20386d to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Data.Entity.Core.Metadata.Edm; | |
using System.Data.Entity.Infrastructure; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Z.BulkOperations; | |
namespace DiamondERP.Helpers | |
{ | |
public static class ExtEFPlusWhere | |
{ | |
public static IQueryable<AuditEntries> Where<T>(this DbSet<AuditEntries> set, T entry) where T : class | |
{ | |
var context = set.GetContext(); | |
var keyNames = context.GetKeyNames<T>(); | |
if (entry == null) | |
{ | |
return set.Where(x => false); | |
} | |
var query = set.Where(x => x.EntityTypeName == typeof(T).Name); | |
foreach (var keyName in keyNames) | |
{ | |
var property = entry.GetType().GetProperty(keyName); | |
var value = property.GetValue(entry, null).ToString(); | |
query = query.Where(x => x.AuditEntryProperties.Any(y => y.PropertyName == property.Name && y.NewValue == value)); | |
} | |
query = query.Include(x => x.AuditEntryProperties).OrderBy(x => x.CreatedDate); | |
return query; | |
} | |
public static List<AuditEntries> Where<T>(this DbSet<AuditEntries> set, params object[] keyValues) where T : class | |
{ | |
var context = set.GetContext(); | |
var keyNames = context.GetKeyNames<T>(); | |
var query = set.Where(x => x.EntityTypeName == typeof(T).Name); | |
if (keyValues == null) | |
{ | |
throw new Exception("Audit_Key_Null"); | |
} | |
if (keyValues.Length != keyNames.Length) | |
{ | |
throw new Exception("Audit_Key_OutOfBound"); | |
} | |
for (var i = 0; i < keyNames.Length; i++) | |
{ | |
var propertyName = keyNames[i].ToString(); | |
var value = keyValues[i] != null ? keyValues[i].ToString() : ""; | |
query = query.Where(x => x.AuditEntryProperties.Any(y => y.PropertyName == propertyName && y.NewValue == value)); | |
} | |
query = query.Include(x => x.AuditEntryProperties).OrderBy(x => x.CreatedDate).Take(5); | |
var lst = query.ToList(); | |
return lst; | |
} | |
public static string[] GetKeyNames<TEntity>(this DbContext context) | |
where TEntity : class | |
{ | |
return context.GetKeyNames(typeof(TEntity)); | |
} | |
public static string[] GetKeyNames(this DbContext context, Type entityType) | |
{ | |
var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace; | |
// Get the mapping between CLR types and metadata OSpace | |
var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace)); | |
// Get metadata for given CLR type | |
var entityMetadata = metadata | |
.GetItems<EntityType>(DataSpace.OSpace) | |
.Single(e => objectItemCollection.GetClrType(e) == entityType); | |
return entityMetadata.KeyProperties.Select(p => p.Name).ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment