English 中文(简体)
不支持的例外, 当我在条款中使用一种方法时
原标题:NotSupportedException when I m using a method in the where clause

我用正赫内特 3.2.0.4000 。 我用正赫内特. Linq 来写此查询 。

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

s 在此定义方法 nonIn () 的定义

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

当我执行此查询时, 我有一个 < code> 不支持的例外 < /code> 错误 :

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

我找到了一个不易读的 non Linq 解决方案,但至少我还是想理解为什么无法建立这样的 Linq 查询 。

提前感谢您的帮助!

最佳回答

您必须使用表达式树将 non 翻译为 nHebertate SQL 查询。

http://fabiomaulo.blogspot.in/2010/07/nhibernate-linq-provider-extension.html" rel=“nofollow”>nhibernate linq 提供商扩展号 是一个良好的起点。

"http://www.codeproject.com/articles/318177/Creating-In-and-notIn-extension-methods-for-Nhiber" rel=“no follow” >此链接 in nount > in 扩展方法。

问题回答

您在 Linq 中的代码最终会被 nhibernate 转换为 SQL 查询 。 您无法使用无法转换为 SQL 代码的方法 。

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

代之以使用:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签