Linq based generic alternate to Predicate<T>?
Posted
by Eric
on Stack Overflow
See other posts from Stack Overflow
or by Eric
Published on 2010-05-06T19:13:25Z
Indexed on
2010/05/06
19:18 UTC
Read the original article
Hit count: 131
I have an interface called ICatalog
as shown below where each ICatalog
has a name and a method that will return items based on a Predicate<Item>
function.
public interface ICatalog
{
string Name { get; }
IEnumerable<IFamily> GetItems(Predicate<Item> predicate);
}
A specific implementation of a catalog may be linked to catalogs in various format such as XML, or a SQL database.
With an XML catalog I end up deserializing the entire XML file into memory, so testing each item with the predicate function does does not add a whole lot more overhead as it's already in memory.
Yet with the SQL implementation I'd rather not retrieve the entire contents of the database into memory, and then filter the items with the predicate function. Instead I'd want to find a way to somehow pass the predicate to the SQL server, or somehow convert it to a SQL query.
This seems like a problem that can be solved with Linq, but I'm pretty new to it. Should my interface return IQueryable instead?
© Stack Overflow or respective owner