Lets say I have a User
with 2 derived entities Student
, Teacher
. I used the TPH method, so I actually have no property at all in my classes to tell me who is a teacher or not.
I have 2 booleans that should allow me to load either student or teacher such as this:
//IQueryable<User>
var query = userRepository.GetUsers();
//Type filtering
if (searchModel.IsStudent)
{
query = query.OfType<Student>();
}
if (searchModel.IsTeacher)
{
query = query.OfType<Teacher>();
}
When this tries to evaluate, I get this error when both are true:
DbIsOfExpression requires an expression argument with a polymorphic result type that is compatible with the type argument.
I already looked at some answers here on SO but they are geared towards 1 type of filtering.
I would then like to do something like this (rough coding):
if(query.ToList().FirstOrDefault() is Student)
{
print "student";
}
The mapping:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Map<Teacher>(m => m.Requires("UserType").HasValue("Teach"))
.Map<Student>(m => m.Requires("UserType").HasValue("Stu"))
.Map<Staff>(m => m.Requires("UserType").HasValue("Staff"));
}
Try this one. It works at least for EF 5.
IQueryable<User> Filter(this IQueryable<User> query,
bool includeTeacher, bool includeStudent)
{
return query.Where(x => includeTeacher && x is Teacher
|| includeStudent && x is Student);
}
This is ugly but it will work for what you need:
if (searchModel.IsStudent && searchModel.IsTeacher)
{
var result = context.Users.OfType<Teacher>()
.Cast<User>()
.Union(context.Users.OfType<Student>());
}
And It will run on one query!! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With