Dapper Extensions (predicates)

Source: Internet
Author: User

Https//github.com/tmsmith/dapper-extensions/wiki/predicatesThe predicate systeminchDapper Extensions isvery simple-to-use. In the examples below we'll use the following model: Public classperson{ Public intId {Get;Set; }  Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; }  Public BOOLActive {Get;Set; }  PublicDateTime DateCreated {Get;Set; }} Simple Fieldpredicate Operationto Create a simple predicate, just create a fieldpredicate and pass it to the query Operati On. Fieldpredicate expects a generic type which allows forStrong typing. In the example below, we is returning all PersonswhereThe Active value isEqual totrue. Codeusing(SqlConnection cn =NewSqlConnection (_connectionstring)) {CN.    Open (); varpredicate = predicates.field<person> (f = f.active, Operator.eq,true); IEnumerable<Person> list = CN. Getlist<person>(predicate); cn. Close ();} Generated sqlselect [person]. [Id], [person]. [FirstName], [person]. [LastName], [person]. [Active], [person]. [DateCreated] from [Person]where ([person].[ Active]=@Active_0) In Clause todo:demonstrate so can pass an IEnumerable asThe value to acheive WHERE x in ('a','b') functionalitycompound predicate (predicate group) Compound predicates is achieved through the use of predicate Group S. For each predicate group, you must choose anoperator(and/or). Each predicate that isAdded to the group would be joined with the specifiedoperator.  Multiple predicate groups can be joined together since each predicate group implements ipredicate.in The example below, we Create a predicate group with an andoperator: Codeusing(SqlConnection cn =NewSqlConnection (_connectionstring)) {CN.    Open (); varpg =NewPredicategroup {Operator = Groupoperator.and, predicates =NewList<ipredicate>() }; Pg. Predicates.add (Predicates.field<Person> (f = f.active, Operator.eq,true)); Pg. Predicates.add (Predicates.field<Person> (f = f.lastname, Operator.like,"br%")); IEnumerable<Person> list = CN. Getlist<person>(PG); cn. Close ();} Generated sqlselect [person]. [Id], [person]. [FirstName], [person]. [LastName], [person]. [Active], [person]. [DateCreated] From [person] WHERE ([person].[ Active]=@Active_0) and ([person].[ LastName] like @LastName_1)) multiple Compound predicates (predicate Group) Since each predicate groups implement Ipredicat E, you can chain them together to create complex compound predicates. In the example below, we create both predicate groups and then joins them together with a third predicate group:codeusing(SqlConnection cn =NewSqlConnection (_connectionstring)) {CN.    Open (); varPgmain =NewPredicategroup {Operator = groupoperator.or, predicates =NewList<ipredicate>() }; varPGA =NewPredicategroup {Operator = Groupoperator.and, predicates =NewList<ipredicate>() }; Pga. Predicates.add (Predicates.field<Person> (f = f.active, Operator.eq,true)); Pga. Predicates.add (Predicates.field<Person> (f = f.lastname, Operator.like,"br%"));    PGMAIN.PREDICATES.ADD (PGA); varPGB =NewPredicategroup {Operator = Groupoperator.and, predicates =NewList<ipredicate>() }; Pgb. Predicates.add (Predicates.field<Person> (f = f.active, Operator.eq,false)); Pgb. Predicates.add (Predicates.field<Person> (f = f.firstname, Operator.like,"pa%",true /* not*/ ));    PGMAIN.PREDICATES.ADD (PGB); IEnumerable<Person> list = CN. Getlist<person>(Pgmain); cn. Close ();} Generated sqlselect [person]. [Id], [person]. [FirstName], [person]. [LastName], [person]. [Active], [person]. [DateCreated] From [Person] WHERE (([person].[ Active]=@Active_0) and ([person].[ LastName] like @LastName_1) OR ([person].[ Active]=@Active_2) and ([person].[ FirstName] not like @FirstName_3)) propertypredicatetodoexists predicatevarsubpred = predicates.field<user> (U = u.email, Operator.eq,"[email protected]");varexistspred = predicates.exists<user>(subpred);varExistinguser = CN. Getlist<user> (existspred,NULL, Tran). FirstOrDefault ();

Dapper Extensions (predicates)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.