I have two entities with common properties. I need to apply some where in my query using the common properties. So I decide to do this:
public interface IContract
{
string Name{get;set;}
}
public class Entity1 : IContract
{
public string Name{get;set;}
}
public class Entity2 : IContract
{
public string Name{get;set;}
}
public class Repository
{
public IQueryable<T> Filter<T>(IQueryable<T> query, Request request) where T : IContract
{
return query.Where(x => x.Name== request.Name);
}
public IQueryable<Entity1> GetEntitity1()
{
return Filter(entities.Entity1, new Request { Name = "X" };
}
public IQueryable<Entity2> GetEntitity2()
{
return Filter(entities.Entity2, new Request { Name = "X" };
}
}
The problem is using this way and after apply .ToList() I receive: NotSupportedException. LINQ to Entities only supports casting EDM primitive or enumeration types.
I there a way to fix it or I need to use the Where(string) of ObjectQuery?
Thanks