3

I have faced this issue many times and I wanted to know the best approach to deal with Where clause in Include. Here is my sample code

IEnumerable<Data.StoreMenu> dataStoreMenus = _context.StoreMenus
                .Include(sm => sm.MenuCategories.Where(x => !x.IsDeleted))
                .Where(sm => sm.StoreId == storeId && !sm.IsDeleted).ToList();

The scenario is I have a menu and Menu has multiple menu categories. Both menu and menu categories has a flag to mark items deleted. Now I want to retrieve only non-deleted menu along with NON-DELETED menu categories.

Jay
  • 299
  • 1
  • 5
  • 20
  • You can do a join between the StoreMenus table and the MenuCategories where IsDeleted is false with .Join() method. I do not think what you want to achieve is possible within .Include(). – DevilSuichiro Aug 22 '16 at 05:00
  • Thanks... just wanted to find alternatives as you provided one to use join() – Jay Aug 22 '16 at 05:09

1 Answers1

0

Create a subquery:

var result = _context.StoreMenus
.Where(sm => sm.StoreID == storeId && !sm.IsDeleted)
.Select(sm=> 
   new { Menu=sm, MenuCats = sm.MenuCategories.Where(smc => !smc.IsDeleted) })
.ToList();

Might want to have a look at something similar: here

Community
  • 1
  • 1
Wurd
  • 465
  • 2
  • 15