Entity Framework Linq join method syntax

703 Views Asked by At

I have the following query which works fine

   var aaa = from d in _context.SubCategories
            join x in _context.CategoryLinks on d.ID equals x.SubCategoryID
            where x.CategoryID == CategoryID
            select new
            {
                 d.ID,
                 d.Name,
                 d.Code
            };

Now I'd like to convert it to linq method syntax, as imo it looks cleaner and instead of 'select new' I want to project it to the DM.SubCategory model.

How could I achieve this? I tried the code below but after typing cl.X it doesn't allow me to select any column enter image description here The CategoryLink table is only to check if there is a link between a category and subcategory. (containing ID, CategoryID & SubCategoryID with corresponding FK's)

Solved after projecting to result, still don't know why visual studio refuses to suggest the cl.X though

_context.SubCategories
           .Join(_context.CategoryLinks,
              sc => sc.ID,
              cl => cl.SubCategoryID,
              (sc, cl) => new { SubCategory = sc, CategoryLinks = cl })
           .Select(x => x.SubCategory);
0

There are 0 best solutions below