Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing func as parameter in Linq to Entities and 'Internal .NET Framework Data Provider error 1025' error

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.

like image 552
LukLed Avatar asked Nov 10 '09 21:11

LukLed


1 Answers

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

like image 97
Alex James Avatar answered Oct 29 '22 01:10

Alex James