jeudi 16 juin 2016

How can I declare IQueryable

Hi there and thanks for having a look at this. I've been searching here and elsewhere for a couple of days and have found some good help on several posts but now I'm stuck. At a high level, what I'm doing is creating a generic method to call a repository load method to retrieve database records from a table defined by class T where T is unknown until run time.

Here's the code in my method:

public JsonResult GetData(string modelTypeString)
{            
    Type modelType = Type.GetType(modelTypeString);
    MethodInfo generic = typeof(Repository).GetMethod("Load");
    MethodInfo genericLoad = generic.MakeGenericMethod(modelType);

    System.Linq.Expressions.Expression<System.Func<BLL.Entities.Unit, object>>[] param2 = 
        new System.Linq.Expressions.Expression<System.Func<BLL.Entities.Unit, object>>[1];
    var returnObjects = genericLoad.Invoke(Repository, param2);
}

And here is the Repository Load method:

public IQueryable<T> Load<T>(params Expression<Func<T, object>>[] includeProperties) where T : class
{
    IQueryable<T> query = entities.Set<T>();
    if (includeProperties != null)
    {
        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
    }
    return query;
}

This code compiles and executes but I can't do any queryable actions on the returnObjects. I'd need to do something like

returnObjects.Select(u => new UnitViewModel
{
    Id = u.Id,
    Name = u.Name,
    Description = u.Description,
    DDLocationId = u.LocationId
})
.ToList();

Thanks in advance for any help you can offer!





Aucun commentaire:

Enregistrer un commentaire