c# - How to use Dapper with Linq -


i'm trying convert entity framework dapper improve data access performance.

the queries use in form of predicates "expression>".

to give example:

i have following code need convert using dapper.

what do:

public async task<list<tmodel>> get(expression<func<tmodel, bool>> query) {     // this.context of type dbcontext     return await this.context.set<tmodel>().where(query).tolistasync(); } 

what i'd do:

public async task<list<tmodel>> get(expression<func<tmodel, bool>> query) {     using (idbconnection cn = this.getconnection)     {         return await cn.queryasync<tmodel>(query);     } } 

my google-fu failing me, can please assist.

edit:

note did find: https://github.com/ryanwatson/dapper.extensions.linq

but can't seem figure out how use it.

firstly, 1 of authors of dapper said, when asked

is there plan make dapper.net compatible iqueryable interfaces?

that

there no plans this. far far outside dapper tries do. far antithetical. dapper core tries friend love sql.

(see https://stackoverflow.com/a/27588877/3813189).

in way, suggest various extension packages nuget may help, have suggested.

i have tried dapperextensions, makes writing query filters in programmatic way little easier - eg.

using system.data.sqlclient; using dapperextensions;  namespace stackoverflowanswer {     class program     {         static void main(string[] args)         {             using (var cn = new sqlconnection("server=.;database=northwnd;trusted_connection=true;"))             {                 var list = cn.getlist<products>(                     predicates.field<products>(f => f.discontinued, operator.eq, false)                 );             }         }          class products         {             public int productid { get; set; }             public string productname { get; set; }             public bool discontinued { get; set; }         }     } } 

i tried dapper.extensions.linq (the package suggested), promises to

builds on providing advanced db access through linq queries. fluid configuration makes setup simplistic , quick.

unfortunately, couldn't far it. there isn't documentation , tests don't seem cover querybuilder, appears class use translate linq expressions dapper extensions predicates (as suggested issue parsing boolean expressions querybuilder). tried following, required add ientity interface dto -

using system; using system.data.sqlclient; using system.linq.expressions; using dapper.extensions.linq.builder; using dapper.extensions.linq.core; using dapperextensions;  namespace stackoverflowanswer {     class program     {         static void main(string[] args)         {             using (var cn = new sqlconnection("server=.;database=northwnd;trusted_connection=true;"))             {                 expression<func<products, bool>> filter = p => !p.discontinued;                 var queryfilter = querybuilder<products>.fromexpression(filter);                  var list = cn.getlist<products>(                     queryfilter                 );             }         }          class products : ientity         {             public int productid { get; set; }             public string productname { get; set; }             public bool discontinued { get; set; }         }     } } 

.. failed @ runtime error

operator not found stackoverflowanswer.program+products

i'm not sure why generating predicate manually (the first example) works querybuilder doesn't..

i it's increasingly looking comments left on question correct, need re-work code away expressions used entity framework. since it's been difficult find information querybuilder class, concerned (even if did working) issues encountered difficult (and bugs may go unfixed).


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -