0

This seems so basic and I can't for the life of me figure it out.

Working with .net MVC5 I have a route /{categoryName}/ and I am trying to get all subcategories in a given {categoryName} to display.

Right now I have and am returning the subcategory id numbers:

var subCategoriesInCategory =
  from cat in db.Categories
  join subcat in db.SubCategories
  on cat.CategoryId equals subcat.CategoryId
  where (cat.CategoryName == categoryName)
  select (subcat);

I'm sure this is basic for someoone proficient in SQL, but ...

Thanks

2 Answers 2

1

Try looking at this from the subcategory angle instead of the category.

var subCategoriesInCategory =
    from subcat in db.SubCategories
    where subcat.CategoryId == somevalue
    select subcat;

You would need to replace somevalue with the CategoryID value of the matching Category.

Or you could bypass the join clause by doing this:

var subCategoriesInCategory =
    from subcat in db.SubCategories
    where subcat.CategoryId == (from cat
        in db.Categories
        where cat.CategoryName == categoryName
        select cat.CategoryId
    ).FirstOrDefault()
    select subcat;
Sign up to request clarification or add additional context in comments.

Comments

0

It looks OK. Try a category you know has subcats

var subCategoriesInCategory =
  (from cat in db.Categories
  join subcat in db.SubCategories
  on cat.CategoryId equals subcat.CategoryId
  where (cat.CategoryName == "ExistingCategory")
  select subcat).FirstOrDefault();
if (subCategoriesInCategory == null ) 
{
   // somethings wrong !
}

or if you just want the subCategory IDs

 ...  select new { subcat.ID } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.