Suppose I have two tables, TableA and TableB. Each record in A has one or more related records in B. Say I want a reusable filter using predicates. I might do something like this (Linq-to-SQL by the way):
private Expression<Func<ARecord, bool>> FilterPredicate()
{
return x => x.Name == "Test";
}
private IQueryable<ARecord> GetRecords()
{
return DataContext.TableA.Where(FilterPredicate());
}
That works fine, but say I wanted to search TableB, but use the same "filter" on the foreign key. I want to accomplish the query below without having to rewrite FilterPredicate for how it relates to TableB.
var query = from b in DataContext.B
where b.A.Name == "Test"
select b;
I'm just wondering if there are any best practices for creating reusable "where" clauses that would help across multiple tables.
Edit - To clarify, I'm not looking for a way to apply the predicate to ARecord and BRecord types. I'm looking for a way (any way, not necessarily along the lines I was already thinking of) to prevent needing this predicate as well:
private Expression<Func<BRecord, bool>> FilterPredicate2()
{
return x => x.A.Name == "Test";
}
Thanks in advance.