Asp.net mvc 3 EF LINQ查询-多对多
我需要一些帮助,从EF4.1查询中获取一些数据 我有一个Asp.net mvc 3 EF LINQ查询-多对多,asp.net-mvc-3,entity-framework-4,Asp.net Mvc 3,Entity Framework 4,我需要一些帮助,从EF4.1查询中获取一些数据 我有一个产品表和一个类别表,它们有多对多关系 我需要按ID选择一个产品,并在其中包含与之关联的类别 我想到了这个: Public Function GetProductByID(ID As Integer) As Core.Entities.Product Implements Core.Interfaces.IProductService.GetProductByID Dim p = ProductRepository.Q
产品
表和一个类别
表,它们有多对多关系
我需要按ID选择一个产品,并在其中包含与之关联的类别
我想到了这个:
Public Function GetProductByID(ID As Integer) As Core.Entities.Product Implements Core.Interfaces.IProductService.GetProductByID
Dim p = ProductRepository.Query.Single(Function(x) x.ID = ID)
p.Categories = CategoryRepository.Query.Where(Function(x) x.Products.Any(Function(y) y.ID = ID)).ToList
Return p
End Function
我相信有更好的办法 如果您的产品
实体上有属性类别
,为什么不使用Include()
?(C#语法):
也看到
var p = ProductRepository.Include(x=> x.Categories)
.Single(x => x.ID == ID);