Say I have two classes:
public class Parent {
public Child Child { get; set; }
}
public class Child {
public string Name { get; set; }
}
And I want to write a function that filters Parents by their child's name containing a substring.
public IQueryable<Parent> ParentsChildNameMatches(IQueryable<Parent> parents, string word)
{
return parents.Where(parent => parent.Child.Name.Contains(word));
}
If I then want to pull out that Expression so I can reuse the Child contains check elsewhere
public Expression<Func<Child, bool> ChildNameMatches(string word)
{
return child => child.Name.Contains(word));
}
How do I then use that in my .Where such that I can rewrite ParentsChildNameMatches?
public IQueryable<Parent> ParentsChildNameMatches(IQueryable<Parent> parents, string word)
{
return parents.Where( // how to leverage ChildNameMatches?
}