I threw down this code because it worked, but I really need to refactor to something acceptable. It accepts a set of query objects (strings that look like productid = 3) then adds them to my query. This only works for logical AND, but I will eventually need a few different logical operators (OR, NOT).

```
-- Idea here is add the where clause to the original query and return a new one
private static IQueryable<Product> GetFilteredQuery(string condition,
    IQueryable<Product> originalQuery)
    {
        -- REPETITION 
        if( -- Regex comparison looking for "productid = 123" --)
        {   
            returnQuery = originalQuery.Where(
                    p => p.myEntity.SelectMany(q => q.subEntity) // spec expression
                          .Any(r => r.id == foundid));           
        }
        ... (one if statement for each specification, calling this several times)

```

I also have this for Ordering:

```
private static IQueryable<Product> GetOrderedQuery( IList<string> fields,
    IQueryable<Product> originalQuery)
{
    var resultQuery = originalQuery;
    bool firstTime = true;
    foreach( var field in fields)
    {   
        -- REPETITION  
        if( field == "id")
        {    if( firstTime == true)
             {   resultQuery = resultQuery.OrderBy( p => p.id);
                 firstTime = false;
             }
             else
             {   resultQuery = resultQuery.ThenBy( p => p.id);
             }
        }
        ... (one for each field to order by)
    }

```

So how could I encapsulate each repetition in to a specification object where I can somehow attach this collection of specifications to my original query, including the order expressions? This is under the Linq to Entities, Entity Framework 4, and C# umbrella.

It would be really nice to do something like this, which is essentially what the above does.

```
var originalQuery = ...;
foreach( var spec in mySpecs)
{    originalQuery = spec(originalQuery);  //adds all the where clauses
}

originalQuery = orderSpec( originalQuery); // adds all the order fields

```

Links to websites, example code, would surely be appreciated.