score:12

Accepted answer

sure. you can write a method like:

public expression<func<customer, bool>> getdave()
{
    return c => c.firstname == "dave"
             && c.isactive
             && c.hasaddress;
}

...and repository methods like:

public ienumerable<customer> getoneguy(expression<func<customer, bool>> criteria)
{
    return context.customers.where(criteria);
}

...and call:

var dave = repository.getoneguy(this.getdave()).single();

score:0

you can build an expression and use it later

var isg = guid.tryparse(id, out glt);
expression<func<yourobjectresulttype, bool>> query = w => w.id == id;

if (isg) {
    query = w => w.token == glt;
}

var result = _context.tblyourtable.where(query);

score:1

if you just want to capture these kinds of queries in a re-usable way, you might implement this as extension methods on iqueryable<customer> (or whatever poco you want). something like:

public static class extnensionsforiqueryablecustomer
{
    public static ienumerable<customer> whereactivewithaddressandnamed (this iqueryable<customer> queryable, string name)
    {
        return queryable.where (c => c.firstname == name)
                        .where (c => c.isactive)
                        .where (c => c.hasaddress);
    }
}

you could then consume this like:

customerrepository.getall ().whereactivewithaddressandnamed ("dave");

Related Query

More Query from same tag