Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Entity Framework with Include and Select together

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?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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 are using an anonymous type in your linq query, the include statements are discarded.
You would have to load all the related ProgramFoodTypes on the client side and then filter:

 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 into a new object in case you want to make sure your data will be intact on the database side.

over 4 years ago · Santiago Trujillo Relatório

0

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);
over 4 years ago · Santiago Trujillo Relatório

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));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda