0

According to this StackOverflow answer:

Linq to Entities - how to filter on child entities

you should be able to filter down the list of related entities in Entity Framework by utilizing a projection, like I've done here:

Company company = _context.Company
.Where(g => g.CompanyId == id)
.Select(comp => new
{
    group = comp,
    operators = comp.Operator,
    formFamilies = comp.FormFamily.Where(ff => ff.IsActive ?? false)
}).AsEnumerable().Select(i => i.group).FirstOrDefault();

To give a quick overview of what I'm trying to obtain here, I'm trying to get a list of all of the active form families associated with this company object, however, whenever I restrict the results in any way, the result set is empty.

  • If the line were formFamilies = comp.FormFamily then it returns two results, one active one inactive
  • If the line is formFamilies = comp.FormFamily.Where(ff => true) then it returns nothing
  • If the line is formFamilies = comp.FormFamily.OrderBy(ff => ff.FormFamilyId) then it returns nothing.

Any sort of modification that I do to comp.FormFamily means the result set returns nothing, I've dug through the deepest sections of SA to try to find a solution, and tried every solution I've found, but nothing seems to cause this list to return anything.

Tân
  • 1
  • 15
  • 56
  • 102

2 Answers2

0

Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.

  var company =  from c in _context.Company
                           join f in _context.FormFamily
                           on c.Id equals f.CompanyId   
                           where c.Id == id
                           select new Company()
                           {
                               Id = c.Id,
                               operators = c.Operator.ToList(),
                               formFamilies = c.FormFamily.Where(x=>x.IsActive == 
                                               false).ToList()
                           } .FirstOrDefault();

Hope this helps.

  • Thank you, I eventually solved the issue by using populating the list with a second query by using: `_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();` But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax. – Conner Phillis Nov 12 '18 at 18:11
  • Join syntax gets a bit weird in method form. Something like this might help you `var company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() { Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), }); ` – Farrukh Manzoor Nov 13 '18 at 10:11
0

I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.

Anyway, you could simply use Include methods instead of projecting.

var company = _context.Company
    .Where(c => c.Id == id)
    .Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
    .ToList();

Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.

Serenkiy
  • 249
  • 3
  • 14