Displaying data from multiple tables in a view as a list - ASP.Net MVC

asp.net-mvc, linq-to-entities, viewmodel

Solution

If there is an association between the two entities you can access the second type using it. The only thing you need to do in that case is use the Include() method to load the association data.

       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(); 
         }

Than for every `LU_LST_CategoryTypeSet category` you can call `category.LU_LST_Level`

Problem

I have the following two tables (basic outline): Tbl_CategoryType ID LevelID Description Tbl_Levels ID Name Basically, I want to present all of the information in the Tbl_CategoryType table while referencing the Tbl_Levels.Name data based on the Tbl_CategoryType.LevelID number. I have tried using a join in my repository as below; ``` 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}); } ``` However, when I call that method there is no type I can assign it to as it doesn't fit into the type of either the Category or Level. I'm assuming I need to do this through a custom viewmodel but can't figure out the steps. Thanks in advance

Original source