Reputation: 297
I have an IList of Type Entity.
I have a string representing the Type of a class derived from Entity.
I need to cast my IList to IQueryable of the Type derived from Entity using the string.
Sadly, the assembly for Entity and derived classes is not the executing assembly.
Upvotes: 1
Views: 1968
Reputation: 971
Here is the approach with dynamic linq and reflection
var list = new List<Company>()
{
new Company { Foo = "1", NoEmployees = 1 },
new Company { Foo = "3", NoEmployees = 3 }
};
MethodInfo method = typeof(Queryable).GetMethods().Where(x => x.Name == "AsQueryable" && x.IsGenericMethod).First();
MethodInfo generic = method.MakeGenericMethod(new Type[] { Type.GetType(typeString) });
var queryable = (IQueryable)generic.Invoke(null, new object[] { list });
var result = queryable.Select("new (NoEmployees)").Where("NoEmployees > 2").OfType<object>().ToList();
Pls be more explicit next time
Upvotes: 1
Reputation: 426
Please provide more information, this is the only thing I can think of that might be what you want.
var queryable = new List<dynamic>();
foreach(var entity in IList<Entity>)
{
dynamic companyProfile = Convert.ChangeType(entity, Type.GetType(typeString));
if (companyProfile.CompanyProfileId == "something")
queryable.Add(companyProfile);
// Do Stuff
}
// Do Stuff
Upvotes: 0