Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will dapper do with a predicate?

Tags:

c#

dapper

If I have a method that returns a List of Dogs, does Dapper convert the Where predicate into a SQL Where? or does it get all the dogs first and then filter the list?

public IList<Dog> GetDogsBy(Func<Dog, bool> predicate)
{
    return db.Query<Dog>("SELECT * FROM Dog",null).Where(predicate).ToList();
}
like image 602
xaisoft Avatar asked Nov 28 '22 23:11

xaisoft


1 Answers

It depends on what overload resolution does when resolving the Where.

If the result of the call to Query is a List<Dog> and you have a using System.Linq; directive, and the argument is a delegate, then the Where will resolve to the LINQ-to-objects version of Where. So the select query will run on the server, and then the Where will run on the client.

If the result of Query is IQueryable<Dog> and the argument is an Expression then overload resolution will choose the Where which combines with the existing query. When that query is executed, the Where runs on whatever side the query providers chooses to run it on, which is typically the server.

like image 168
Eric Lippert Avatar answered Dec 08 '22 12:12

Eric Lippert