简体   繁体   English

使用动态Linq实体框架查询抛出的奇怪异常

[英]Strange Exception thrown using Dynamic Linq Entity Framework Query

I have a gallery entity framework class,and I'm attempting to use the Dynamic Linq Library posted on ScottGu's blog to query the entity set. 我有一个gallery实体框架类,我试图使用ScottGu博客上发布的Dynamic Linq Library来查询实体集。 The failing line of code reads: 失败的代码行读取:

return context.Galleries.OrderBy(sidx + " " + sord).Skip(page * rows).Take(rows).ToList();

sidx=="Name", and sord=="desc". sidx ==“名称”,和sord ==“desc”。

The Gallery object does have a property called "Name". Gallery对象确实有一个名为“Name”的属性。 However, when executed, i get the following exception: 但是,执行时,我得到以下异常:

'Title' could not be resolved in the current scope or context. 无法在当前范围或上下文中解析“标题”。 Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly., near simple identifier, line 6, column 1. 确保所有引用的变量都在范围内,加载了所需的模式,并且正确引用了名称空间。,接近简单标识符,第6行,第1列。

Does anyone know what this means? 有谁知道这意味着什么?

"it" alias was the problem so the following code should works: “它”别名是问题所以以下代码应该工作:

prefix your filter field name Title as it.Title 将过滤器字段名称标题it.Title

I found the answer here .. http://challenge-me.ws/?tag=/Exceptions 我在这里找到答案.. http://challenge-me.ws/?tag=/Exceptions

use:AsQueryable <>

return context.Galleries.AsQueryable().OrderBy(sidx + " " + sord).Skip(page * rows).Take(rows).ToList();

I found a fix, but it doesnt explain the original issue. 我找到了修复,但它没有解释原始问题。 The query was in a library, and being referenced from the asp.net mvc application. 查询位于库中,并从asp.net mvc应用程序中引用。 It compiled fine, but bombed at runtime. 它编译得很好,但在运行时遭到轰炸。 The fix was putting the dynamiclinq class in the mvc app itself, returning a plain IQueryable form the library, and doing the filtering in the controller itself. 修复程序是将dynamiclinq类放在mvc app本身中,从库中返回一个简单的IQueryable表单,并在控制器本身中进行过滤。 The exact same code worked there. 完全相同的代码在那里工作。 Somehow the separation in the library caused the issue. 不知何故,图书馆的分离导致了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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