动态 Linq 不适用于 Linq to Entities

发布于 2024-11-26 15:30:55 字数 770 浏览 0 评论 0原文

我正在开发 Linq To 实体的小项目。需要根据用户设置的搜索条件/过滤器来过滤数据。由于过滤/搜索条件可以适用于视图中显示的任何字段,因此我决定使用 Dynamic Linq。

我从 Scotgu 的博客。在我的类中引用了 System.Linq.Dynamic,我创建了一个示例类,它执行如下所示的动态 linq 查询,

AdventureWorksEntities adent = new AdventureWorksEntities();

var q = adent.
Contacts.OrderBy("FirstName").
Select("New(Title, FirstName, LastName)");

var l = q.ToList();

但是,在上面的代码块中,当查询执行时,我收到错误,

“FirstName”无法在当前范围或上下文中解决。确保所有引用的变量都在范围内,加载所需的架构,并且正确引用命名空间。接近简单标识符,第 8 行,第 1 列。

我已经检查了很多关于这方面的帖子,但帮助很少...

任何有关如何解决上述错误的指示都会很棒...谢谢...

PS 对 DataContext 的类似查询工作正常......

I am working on small project which Linq To entities. There is a requirement to filter the data based on the search criteria/filter which is set by the user. Since the filter/search criteria can be for any of the fields which are displayed in the view, I decided to use Dynamic Linq.

I downloaded the library from Scotgu's blog. Referenced the System.Linq.Dynamic in my class and i have created a sample class which does a dynamic linq query like the below,

AdventureWorksEntities adent = new AdventureWorksEntities();

var q = adent.
Contacts.OrderBy("FirstName").
Select("New(Title, FirstName, LastName)");

var l = q.ToList();

However, in the above code block, when the query executes, I get the error,

'FirstName' 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 8, column 1.

I have check quite a few posts on this regard but with very less help...

Any pointers on how to resolve the above error will be great... Thanks...

P.S. A similar query on the DataContext is working fine...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-12-03 15:30:55

使用以下代码进行测试:

var q = adent.

Contacts.OrderBy("it.FirstName")。
选择(“新(标题,名字,姓氏)”);

Test with this code :

var q = adent.

Contacts.OrderBy("it.FirstName").
Select("New(Title, FirstName, LastName)");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文