secretAgentB
secretAgentB

Reputation: 1279

How to use Func with IQueryable that returns IOrderedQueryable

I'm doing some research about EF and came across a function that accepts

Func<IQueryable<Student>, IOrderedQueryable<Student>> 

and just wondering how to call that function that accepts that kind of parameter?

Upvotes: 13

Views: 20082

Answers (2)

Amy B
Amy B

Reputation: 110171

define a method.

public IOrderedQueryable<Student> OrderingMethod(IQueryable<Student> query)
{
  return query.OrderBy(student => student.Name);
}

Now this assignment is legal:

Func<IQueryable<Student>, IOrderedQueryable<Student>> orderingFunc = this.OrderingMethod;

And now that you have it in a variable, it's easy to pass it to the method.


You could also do it all inline:

Func<IQueryable<Student>, IOrderedQueryable<Student>> orderingFunc =
  query => query.OrderBy(student => student.Name);

Upvotes: 4

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60503

imagine function is something like that, and you've got a property Id in Student class.

public static class Helper {
    public static void Test(Func<IQueryable<Student>, IOrderedQueryable<Student>> param)
        {
            var test = 0;
        }
}

then you could use it this way

var student = new List<Student>().AsQueryable();//non sense, just for example
Helper.Test(m => student.OrderBy(x => x.Id));

m => student.OrderBy(x => x.Id) is a

Func<IQueryable<Student>, IOrderedQueryable<Student>>

(IQueryable<student> as parameter, returning a IOrderedQueryable<Student>)

or just

Helper.Test(m => m.OrderBy(x => x.Id));

In fact this doesn't make much sense without a "real" function...

Upvotes: 13

Related Questions