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();


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);


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