0

I have an EF DB context query that produces a list of objects that look like this:

public interface Parent
{
    public string Name { get; set; }
    public virtual ICollection<Child> Children {get; set;}
}

public interface Child 
{
    public string Name {get; set;}
    public boolean isValid {get; set;}
}

The query itself looks like

var list _dbContext.Parents.Include(x => x.Children).ToList();

I want to be able to query each parent, pull out the details of each child that has isValid = true and then store them all in a list, but I can't figure out the syntax.

I was trying to do something like

list.Where(x => x.Children.Where(child => child.isValid).toList()).toList();

But the above just produces an IList<IList>

Jake12342134
  • 1,539
  • 1
  • 18
  • 45

1 Answers1

-1

I've accomplished that using an expression like this:

var list.Where(x => x.Children.Any(child => child.isValid)).toList();

Keyword Any is very powerful in this cases

Herberth Gomez
  • 187
  • 1
  • 2
  • 19