Search code examples
c#asp.netentity-frameworkdynamiclinq-to-entities

Initializing (Entity) object by calling (get) property dynamically. Entity Framework , c#, ASP>NET


I have below piece of code:

public void DBMamLookup(int custid)
{
    using (LookUpEntities1 lookUp = new LookUpEntities1())
    {
        
        var mamconfig = lookUp.MamConfigurations;
        var userlookup = lookUp.UserAccount2_ISO2_3 ;
        
        MamConfiguration obj = mamconfig.Where(m => m.CustID== custid).FirstOrDefault();
        var objNN = lookUp.UserAccount2_ISO2_3.Where(m => m.CustID== custid).Take(15).ToList();            
        
        Type returnType;
        switch (obj.ActiveTableName)
        {
            case "MamConfiguration":
                returnType = typeof(MamConfiguration);
                break;                                            
            case "UserAccount1_ISO1_1Billers":                        
                returnType = typeof(UserAccount1_ISO2_3Billers);
                break;                    
            default:
                returnType = typeof(UserAccount2_ISO2_3Billers);
                break;
        }
        dynamic que3 = this.GetInstance<dynamic>(obj.ActiveTableName);                
        que3 = lookUp.Set(returnType);
        
        for (int i = 0; i < que3.Local.Count; i++)
        {
            Console.WriteLine(que3.Local[i].UserAccount);
        }       
    }
}        

I have problem at below line in above code:

var objNN = lookUp.**UserAccount2_ISO2_3**.Where(m => m.CustID== custid).Take(15).ToList(); 

I have to make it dynamic and call the specific entity property at runtime. As I have property name in string i.e. obj.ActiveTableName how can I make a call something like below:

var objNN = lookUp.**[obj.ActiveTableName]**.Where(m => m.CustID== custid).Take(15).ToList(); 

Solution

  • First create an interface with common properties for all types: For example:

    interface IEntityWithCustID
    {
        int CustID { get; set; }
    }
    

    Make sure all relevant classes implement that interface For example:

    public class UserAccount2_ISO2_3 : IEntityWithCustID
    

    Create a helper class to retrieve data

    static class QueryHelper
    {
        public static List<IEntityWithCustID> GetResult(LookUpEntities1 lookup, string tableName, int custId)
        {
            var dbset = lookup.GetType().GetProperty(tableName).GetValue(lookup);
            var entityType = dbset.GetType().GetGenericArguments()[0];
            var method = typeof(QueryHelper).GetMethod(nameof(GetResultInternal)).MakeGenericMethod(entityType);
            return (List<IEntityWithCustID>)method.Invoke(null, new object[] { dbset, custId });
        }
    
        public static List<IEntityWithCustID> GetResultInternal<T>(IDbSet<T> dbset, int custId) where T: class, IEntityWithCustID
        {
            return dbset.Where(m => m.CustID == custId).Take(15).ToList().Cast<IEntityWithCustID>().ToList();
        }
    }
    

    Use that class from your code (For example)

    var res = QueryHelper.GetResult(lookup, obj.ActiveTableName, custid);
    

    There is another way where you can create Expression.Lambda to create custom lambda expression using Expression.Lambda call. It will be a little bit more complicated though.