I am trying to build an Entity Framework linq query to return the parent object from a child object.
The models look like this:
public class Parent
{
Guid Id {get; set;}
List<Child> Children {get; set;}
}
public class Child
{
Guid Id {get; set;}
}
And my query looks like this:
string _foreignKeyName = "Children"
Guid existingChildId = "{some existing guid}"
var parent = _context.Set<Parent>()
.Include(_foreignKeyName)
.Where(x => x.Children // <--- I would like to make "Children" dynamic
.Where(y => y.Id == existingChildId).Any())
.FirstAsync();
Is there anyway to make the reference to "Children" dynamic and use {_foreignKeyName} instead?
I've looked at Expression Trees and Dynamic Linq, but I'd much rather keep it in standard linq if possible.
Thanks!