VB.NET Linq to Entities 查询子对象
基本上我有以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,
.Contains()
需要一个Role
,并且您向它传递一个列表。这就是错误所说的。尝试一下(如果我破坏了 VB.NET 语法,请提前道歉;我通常不写 VB.NET):
Well,
.Contains()
expects oneRole
, 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):