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

All we need is an easy explanation of the problem, so here it is.

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);
            case "UserAccount1_ISO1_1Billers":                        
                returnType = typeof(UserAccount1_ISO2_3Billers);
                returnType = typeof(UserAccount2_ISO2_3Billers);
        dynamic que3 = this.GetInstance<dynamic>(obj.ActiveTableName);                
        que3 = lookUp.Set(returnType);
        for (int i = 0; i < que3.Local.Count; i++)

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(); 

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply