Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 EF LINQ查询-多对多_Asp.net Mvc 3_Entity Framework 4 - Fatal编程技术网

Asp.net mvc 3 EF LINQ查询-多对多

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

我需要一些帮助,从EF4.1查询中获取一些数据

我有一个
产品
表和一个
类别
表,它们有多对多关系

我需要按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);