实体框架(3.5):如何将某个 LINQ 查询转换为 eSQL?

发布于 2024-08-25 10:03:20 字数 518 浏览 8 评论 0原文

我有以下 LINQ 查询,需要将其转换为 Entity SQL /eSQL):

return (ObjectQuery<User>) from user in Users
   where !user.Roles.Any(r => r.AnIntegerProperty < 0)
   select user;

User.Roles 是与 Roles 的 n:m 关系的导航属性,反之亦然还有一个 Role.Users 导航属性。模型中没有可用的 User_Roles 或 Roles_User 实体,我无法添加它们。

我也不能在这里使用 LINQ 语句,因为我需要添加 .OrderBy("it." + propertyname) (来自另一个源,也无法更改它),如果构建了 ObjectQuery,这是不可能的与 linq 。

那么如何将其转换为 eSQL 呢?我在哪里可以找到好的 eSQL 示例?到目前为止,我搜索了一整天,必须承认 eSQL 参考很糟糕,并且网络上没有任何可用的示例。

I have the following LINQ query that I need to translate to Entity SQL /eSQL):

return (ObjectQuery<User>) from user in Users
   where !user.Roles.Any(r => r.AnIntegerProperty < 0)
   select user;

User.Roles is an navigation property to the n:m relation to Roles and there also is a Role.Users navigation property the other way round. There aren't User_Roles or Roles_User Entities available in the model, and I can't add these.

I also can't use the LINQ statement here, because I need to add .OrderBy("it." + propertyname) (comes from another source, can't change that too) later on which is not possible if the ObjectQuery is build with linq.

So how do I translate this to eSQL? And where can I find good eSQL samples? I searched for a whole day until now and must admit that eSQL reference is lousy and there aren't any usable examples around the web.

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

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

发布评论

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

评论(4

凝望流年 2024-09-01 10:03:20

如果您还没有找到解决方案,这将起作用

SELECT VALUE u FROM YourDataContextEntities.Users AS u WHERE NOT EXISTS(SELECT r FROM u.Roles AS r WHERE r.AnyIntegerProperty < 0)

In case you haven't find solution, this will work

SELECT VALUE u FROM YourDataContextEntities.Users AS u WHERE NOT EXISTS(SELECT r FROM u.Roles AS r WHERE r.AnyIntegerProperty < 0)
似最初 2024-09-01 10:03:20

我认为动态 linq 库可能是这里的解决方案:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic -query-library.aspx

您可以使用动态属性名称创建过滤表达式,因此无需进行翻译。

I think that dynamic linq library may be solution here:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

You can create filtering expressions using dynamic property names, so there is no need to make translations.

你的他你的她 2024-09-01 10:03:20

我会写一些类似

contexte.Users.Where("NOT EXISTS (SELECT VALUE r FROM it.Roles AS r WHERE  r.AnIntegerProperty < 0)")

未测试的东西,但我已经尝试过类似的东西,所以这应该适合你。

I'd write it something like

contexte.Users.Where("NOT EXISTS (SELECT VALUE r FROM it.Roles AS r WHERE  r.AnIntegerProperty < 0)")

not tested but I've already tried something similar so thi should work for you.

失退 2024-09-01 10:03:20

如果不了解用户和角色可用的具体信息,就很难找到答案。但是,鉴于您所说的,以下内容会起作用:

return (ObjectQuery<User>) from user in Users
                            where !(from role in dataContext.Roles
                                    where role.AnIntegerProperty < 0
                                    select role.UserId).Contains(user.UserId);

It is difficult to find an answer without knowing the specifics of what is available on Users and Roles. However, given what you have said, will the following work:

return (ObjectQuery<User>) from user in Users
                            where !(from role in dataContext.Roles
                                    where role.AnIntegerProperty < 0
                                    select role.UserId).Contains(user.UserId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文