How to add more query on to the end of an IEnumerable

I would like to pass part of the query through as a parameter of a method.

public IEnumerable<ServiceModel> Get(/*What would the type be*/ rightQuery)
{
    IEnumerable<EntityModel> leftQuery = repository.GetAll;
    // the query appending might look something like this: 
    var outcomeBeforeMapping = leftQuery.rightQuery;

    // ...rest is not important
}

The call might look like this:

service.Get((query) => query.OrderByDescending(project => project.SortOrder));

How do i go about doing this?

Jon Skeet
people
quotationmark

It sounds like you just want a Func<IEnumerable<ServiceModel>, IEnumerable<ServiceModel>> - in other words, "Given one query, I'll give you another."

public IEnumerable<ServiceModel> Get(
    Func<IEnumerable<ServiceModel>,  IEnumerable<ServiceModel>> transform)
{
    IEnumerable<EntityModel> leftQuery = repository.GetAll;
    var outcomeBeforeMapping = transform(leftQuery);
    ...
}

You should consider using IQueryable<T> instead of IEnumerable<T> for this if you're using EF etc, to allow the transformation to affect the SQL rather than only being applied locally.

You should also consider simply separating out the first part of your operation from the second - allow a user to call GetAll(), do what they want, then pass the result back into a second method.

people

See more on this question at Stackoverflow