18

We have a class called Task:

public partial class Task : EntityObject
{
    public EntityCollection<TaskUser> TaskUsers { get {...} set{...} } 
}

It has navigation property called TaskUsers, which contains users attached to this taks:

public partial class TaskUser : EntityObject
{
    public User User { get {...} set {  } }
}

Every TaskUser object has User object.

We are given IQueryable<Task> tasks. We want to find tasks assigned to user with ID = 1. When we use

tasks.Where(t => t.TaskUsers.Any(a => a.User.ID == 1))

everything works fine. When we use

Func<TaskUser, bool> function = a => a.User.ID == 1;
return tasks.Where(t => t.TaskUsers.Any(function));

we get nice 'Internal .NET Framework Data Provider error 1025' error. Why? I want to build much more complicated filters using Expression class, but if I can't pass simple Func, this can't be done. What should I do?

EDIT

Maybe

Func<TaskUser, bool> function = a => a.User.ID == 1;
return tasks.Where(t => t.TaskUsers.Any(function));

doesn't work, but

Expression<Func<TaskUser, bool>> expression = a => a.User.ID == 1;
return tasks.Where(t => t.TaskUsers.AsQueryable().Any(expression));

works! That is all I needed.

LukLed
  • 31,452
  • 17
  • 82
  • 107
  • Heh, I just posted a bounty on a [similar question](http://stackoverflow.com/questions/11990158/internal-net-framework-data-provider-error-1025), before finding your solution here. Wanna go grab the bounty? – Shaul Behr Dec 13 '12 at 13:36
  • Actually, having tried using `Expression` rather than `Func`, like you did, mine did **not** work. Any other ideas? – Shaul Behr Dec 13 '12 at 13:52

1 Answers1

26

Well the EF can only translate Expressions, not functions.

i.e. it can translate this:

Expression<Func<TaskUser,bool>> 

but not this:

Func<TaskUser,bool>

As for how to merge expressions (in pseudo code):

Expression<Func<TaskUser, bool>> expression = a => a.User.ID == 1;
return tasks.Where(t => t.TaskUsers.Any(expression));

There are probably some Expression guru's who can help with that.

I suggest a followup question focused on that particular problem

Alex

Alex James
  • 20,874
  • 3
  • 50
  • 49
  • 12
    Without help of guru: return tasks.Where(t => t.TaskUsers.AsQueryable().Any(expression)); – LukLed Nov 11 '09 at 11:55