以列表形式显示视图中多个表的数据 - ASP.Net MVC

shi*_*hif 5 asp.net-mvc linq-to-entities viewmodel

我有以下两个表(基本大纲):

Tbl_CategoryType

ID LevelID描述

Tbl_Levels ID名称

基本上,我想在Tbl_CategoryType表中提供所有信息,同时根据Tbl_CategoryType.LevelID号引用Tbl_Levels.Name数据.

我尝试在我的存储库中使用连接,如下所示;

public IQueryable GetAllTypesInCategory(int CatID)
{
     return (from x in DBEntities.LU_LST_CategoryTypeSet
             where x.CategoryID == CatID && x.Enabled == 1
             join y in DBEntities.LU_LST_LevelSet on x.LevelID equals y.ID
             select new {x, y});
}
Run Code Online (Sandbox Code Playgroud)

但是,当我调用该方法时,没有类型我可以将其分配给它,因为它不适合类别或级别的类型.

我假设我需要通过自定义视图模型执行此操作,但无法弄清楚步骤.

提前致谢

Bra*_*nov 3

如果两个实体之间存在关联,您可以使用它访问第二种类型。在这种情况下,您唯一需要做的就是使用 Include() 方法加载关联数据。

       public List<LU_LST_CategoryType> GetAllTypesInCategory(int CatID)  
         { 
             return (from x in DBEntities.LU_LST_CategoryTypeSet.Include("LU_LST_LevelSet") 
                     where x.CategoryID == CatID && x.Enabled == 1  
                     select x).ToList(); 
         }
Run Code Online (Sandbox Code Playgroud)

LU_LST_CategoryTypeSet category你能打电话的每一个电话category.LU_LST_Level