15

I have the following entities ( pseudo code to save space)

Program [ int Id, 
          string Name, 
          List<ProgramFoodType> ProgramFoodTypes, 
          List<ProgramFood> ProgramFoods]

ProgramFoodType[ int Id, int ProgramId, int Type, bool IsActive]
ProgramFood [ int Id, int ProgramId, Food Food, FoodType FoodType]
Food [int Id, string Name]
FoodType [int Id, string Name]

my task is to get single Program with its related ProgramFoodTypes with condition ProgramFoodType should be active and ProgramFoods with related entities Food and FoodType

I used the following so far

1- the below query will retrieve the details of ProgramFoodTypes and ProgramFoods but it will bring all active and inactive ProgramFoodTypes

var program = mEntities.Programs
                          .Include(p =>p.ProgramFoodTypes)
                          .Include(p =>p.ProgramFoods.Select(f =>f.Food))
                          .InClude(p =>p.ProgramFoods.Select( f =>f.FoodType))
                          .Where(m =>m.Id== Id);

2- the below query will retrieve the details but missing the Food and FoodType

var program = (from p in mEntities.Programs
              where p.Id ==Id
              select new {
                 Program = p,
                 ProgramFoodTypes = from pf in p.ProgramFoodTypes
                                    where pf.IsActive
                                    select pf,                  
                 ProgramFoods = p.ProgramFoods // here i can't add include statement
              }).ToArray().Select(m => m.Program);

how to include the food and food type in the second query?

3 Answers 3

4

For your second solution, I think you can use:

var program = (from p in mEntities.Programs
                  .Include(p => p.ProgramFoods.Select(f => f.Food))
                  .Include(p => p.ProgramFoods.Select(f => f.FoodType))
               where p.Id == Id
               select new {
                  Program = p,
                  ProgramFoodTypes = from pf in p.ProgramFoodTypes
                                     where pf.IsActive
                                     select pf,                  
                  p.ProgramFoods 
               }).ToArray().Select(m => m.Program);

update: Since you're using anonymous type in your linq query, Include statements are dismissed.
You'd have to load all related ProgramFoodTypes on client side, and then do the filtering:

var program = mEntities.Programs
                   .Include(p => p.ProgramFoodTypes)
                   .Include(p => p.ProgramFoods.Select(f => f.Food))
                   .Include(p => p.ProgramFoods.Select(f => f.FoodType))
                   .SingleOrDefault(m => m.Id == Id);

program.ProgramFoodTypes = program.ProgramFoodTypes.Where(pft => pft.IsActive);  

You can use AsNoTracking() or clone the returned Program object in a new object in case you want to make sure your data will be intact on db-side.

3
  • the Food and FoodType still empty in the result
    – Monah
    Jul 8, 2015 at 11:50
  • Ahh I see. That's because you selected anonymnous oobject not the entity itself. updating my answer...
    – Kamyar
    Jul 8, 2015 at 11:56
  • I was trying to avoid splitting the query and to make it simple, the missing part was that i can Include the food and food type in the anonymous child query, as @tschmit007 mentioned in his answer, thank you for your updates, i think it will work since it was splitted.
    – Monah
    Jul 8, 2015 at 12:12
3

may be:

var program = (from p in mEntities.Programs
          where p.Id ==Id
          select new {
             Program = p,
             ProgramFoodTypes = from pf in p.ProgramFoodTypes
                                where pf.IsActive
                                select pf,                  
             ProgramFoods = p.ProgramFoods.Select(y => new {
                 Food = y.Food,
                 Type = y.FoodType
             }) 
          }).ToArray().Select(m => m.Program);
1
  • It worked fine, just i added the ProgramFood it self or it will not be retrieved, thank you
    – Monah
    Jul 8, 2015 at 12:09
0

Try this:

var program = mEntities.Programs
                       .Include(p => p.ProgramFoodTypes)
                       .Include(p => p.ProgramFoods.Select(f => f.Food))
                       .InClude(p => p.ProgramFoods.Select(f => f.FoodType))
                       .SingleOrDefault(m => m.Id == Id && m.ProgramFoodTypes.All(t => t.IsActive));
4
  • it returned System.NullReferenceException ( Object reference not set to an instance of an object).
    – Monah
    Jul 8, 2015 at 11:47
  • If you mean that program is null, that's because no results found that are matching the criteria.
    – haim770
    Jul 8, 2015 at 11:58
  • I think what OP is trying to achieve is to fetch the object but only related ProgramFoodTypes that their IsActive are set to true.
    – Kamyar
    Jul 8, 2015 at 12:04
  • I am getting the result normally but not like what i want when i remove the m.ProgramFoodTypes.All(t=>t.IsActive), it seems the where clause was applied before actually including the entities? something like this
    – Monah
    Jul 8, 2015 at 12:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.