Overview:
I am currently working on a GraphQL API and I'm writing a fluent interface for creating joins between database entities inside my application and I would like to create some reusable "blocks" that use generic repository.
The problem:
Provided that I have a Func<TEntity, TKey>
that will be used to select a field from the entity, is there some easy way to use it inside a Where
expression to construct a condition using that field?
Plese see the pseudocode below to see what I mean:
public class Repository<TEntity> {
private Func<TEntity, Guid> keySelector;
// ...
public List<TEntity> GetEntitiesByKeys(Guid parentId, IEnumerable<Guid> keys) {
return context.Set<TEntity>
.Where(x => x.ParentId == parentId && keys.Contains(keySelector(x)) // Can I do something to use the keySelector here?
.ToList();
}
}