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

I have the following two tables (main outline):

Tbl_CategoryType

ID LevelID Description

Tbl_Levels I WOULD Name

Basically, I want to present all the information in the Tbl_CategoryType table, referring to the Tbl_Levels.Name data based on the Tbl_CategoryType.LevelID number.

I tried using the connection 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 this method, I cannot assign it because it does not fit into the category or level type.

I guess I need to do this with a custom view model, but can't figure out the steps.

Thank you in advance

+5
source share
2

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

LU_LST_CategoryTypeSet category category.LU_LST_Level

+3

linq:

select new {x, y}

, Entity.

, EntityFramework - , . true, , ViewModel.

-, .

public class MyViewModel
{
    public MyViewModel(LU_LST_CategoryTypeSet x, LU_LST_LevelSet y)
    {
        Category = x;
        Level = y;
    }

    public LU_LST_CategoryTypeSet Category { get; set;}
    public LU_LST_LevelSet Level { get; set; }
}

Linq MyViewModel:

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

:

   var listOfTypes = GetAllTypesInCategory(catID);
   foreach (var item in listOfTypes)
   {
      var model = new MyViewModel(item.x, item.y);

      //Do whatever with the model to get it to the view.
   }

MyViewModel.

+4

All Articles