Entity Framework with Include and Select together

asp.net-mvc c# ef-code-first entity-framework

Question

The following entities are mine: ( 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 goal is to become single.Program together with associatedProgramFoodTypes ProgramFoodType must be active, with the caveatProgramFoods along with associated partiesFood and FoodType

I have so far utilized the following

1- The following query will retrieve information aboutProgramFoodTypes and ProgramFoods however, it will bring both active and inactive people.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 following query will retrieve the information, but it willFood 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 can I make the second query contain the food and food type?

1
10
7/8/2015 1:26:43 PM

Accepted Answer

ZZZ_tmp
1
7/8/2015 11:58:04 AM

Popular Answer

My suggestion for your second solution is to 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);

You're using anonymous type in your linq query, therefore update: Include claims are rejected.
You would need to load all relevantProgramFoodTypes then perform the filtering on the client side:

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 may utilizeAsNoTracking() or duplicate the receivedProgram if you want to ensure that your data will be preserved on the database side, object in a new object.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow