VB.NET Linq to Entities 查询子对象

发布于 2024-12-10 05:40:21 字数 882 浏览 0 评论 0原文

基本上我有以下内容:

Dim ctx As New AdminCoreEntities
Dim roles = (From r In ctx.Roles where r.Name.StartsWith("cust") Select r) 'list of System.Linq.IQueryable(Of AdminCoreModel.Role)

 Dim items = From i In ctx.QuickLinks.Include("Roles")
             Where (i.TenantID = "470556ba-3574-4b01-a619-b85e9721b966" AndAlso i.Roles.Contains(roles))
             Select New With {
                               i.ID,
                               i.Name,
                               .Roles = (From r In i.Roles Select New With {.Id = r.ID, .Name = r.Name})
                              }

当我运行此命令时遇到的错误是:

Unable tocast object of type System.Data.Objects.ObjectQuery`1[AdminCoreModel.Role] to type AdminCoreModel.Role

基本上我有很多对很多的情况,我尝试获取按角色查询的所有 Quicklinks 对象 并且不太确定当 i.Roles 是对象集合时,为什么 EF 会转换为单个 AdminCoreModel.Role。

非常感谢任何帮助

Basically I have the follwing:

Dim ctx As New AdminCoreEntities
Dim roles = (From r In ctx.Roles where r.Name.StartsWith("cust") Select r) 'list of System.Linq.IQueryable(Of AdminCoreModel.Role)

 Dim items = From i In ctx.QuickLinks.Include("Roles")
             Where (i.TenantID = "470556ba-3574-4b01-a619-b85e9721b966" AndAlso i.Roles.Contains(roles))
             Select New With {
                               i.ID,
                               i.Name,
                               .Roles = (From r In i.Roles Select New With {.Id = r.ID, .Name = r.Name})
                              }

The error i get when i run this is:

Unable to cast object of type System.Data.Objects.ObjectQuery`1[AdminCoreModel.Role] to type AdminCoreModel.Role

Basically I have a many to many situation and I try to get all the Quicklinks objects queried by their roles
and not quite sure why EF will cast to a single AdminCoreModel.Role when i.Roles is a collections of objects.

Any help greatly appreciated

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

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

发布评论

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

评论(1

倾`听者〃 2024-12-17 05:40:21

嗯,.Contains() 需要一个Role,并且您向它传递一个列表。这就是错误所说的。

尝试一下(如果我破坏了 VB.NET 语法,请提前道歉;我通常不写 VB.NET):

Dim ctx As New AdminCoreEntities
Dim items = From i In ctx.QuickLinks  ' You don't need Include because you're projecting.
            Where (i.TenantID = "470556ba-3574-4b01-a619-b85e9721b966" 
                   AndAlso i.Roles.Any(Function(role) role.Name.StartsWith("cust"))
            Select New With {
                                i.ID,
                                i.Name,
                                 .Roles = (From r In i.Roles Select New With {.Id = r.ID, .Name = r.Name})
                          }

Well, .Contains() expects one Role, and you're passing it a list. That's what the error says.

Try (apologies in advance if I mangle the VB.NET syntax; I don't usually write VB.NET):

Dim ctx As New AdminCoreEntities
Dim items = From i In ctx.QuickLinks  ' You don't need Include because you're projecting.
            Where (i.TenantID = "470556ba-3574-4b01-a619-b85e9721b966" 
                   AndAlso i.Roles.Any(Function(role) role.Name.StartsWith("cust"))
            Select New With {
                                i.ID,
                                i.Name,
                                 .Roles = (From r In i.Roles Select New With {.Id = r.ID, .Name = r.Name})
                          }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文