简体   繁体   中英

NotSupportedException when I'm using a method in the where clause

I'm using nHibernate 3.2.0.4000. I wrote this query using nHibernate.Linq

var entities = (from t in this.Session.Query<Task>()
                where NotIn(t, role.Tasks)
                select t).ToList();

Here's the definition of the method NotIn()

private bool NotIn(Task t, IEnumerable<TaskDto> tasks)
{
    foreach (var task in tasks)
    {
        if (t.Name == task.Name) return false;
    }
    return true;
}

When I'm executing this query, I've got an NotSupportedException error:

Boolean NotIn(Probel.NDoctor.Domain.DAL.Entities.Task, System.Collections.Generic.IEnumerable`1[Probel.NDoctor.Domain.DTO.Objects.TaskDto])

I found a non Linq solution that is less readable but I still want to, at least, understand why it is impossible to build a Linq query like this.

Thank you in advance for your help!

You have to translate NotIn to nHibernate SQL query using expression tree.

nhibernate linq provider extension is a good starting point.

this link has In and NotIn extension method for nHibernate.

Your code in Linq is ultimately translated to SQL query by nhibernate. You can not use a method which could not be translated into SQL code.

NHibernate cannot decompile and then parse your code to get valid SQL. There is no way to generate an sql statement from your method.

Instead use:

var entities = (from t in this.Session.Query<Task>()
                where !role.Tasks.Any(rt => rt.Name == t.Name)
                select t).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM