https://github.com/tmsmith/Dapper-Extensions/wiki/Predicates

The predicate system in Dapper Extensions is very simple to use. In the examples below we will use the following model:

public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Active { get; set; }
public DateTime DateCreated { get; set; }
}
Simple FieldPredicate Operation To create a simple predicate, just create a FieldPredicate and pass it to the query operation. FieldPredicate expects a generic type which allows for strong typing. In the example below, we are returning all Persons where the Active value is equal to true. Code using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
IEnumerable<Person> list = cn.GetList<Person>(predicate);
cn.Close();
}
Generated SQL SELECT
[Person].[Id]
, [Person].[FirstName]
, [Person].[LastName]
, [Person].[Active]
, [Person].[DateCreated]
FROM [Person]
WHERE ([Person].[Active] = @Active_0)
IN Clause TODO: Demonstrate that you can pass an IEnumerable as the value to acheive WHERE x IN ('a','b') functionality Compound Predicate (Predicate Group) Compound predicates are achieved through the use of predicate groups. For each predicate group, you must choose an operator (AND/OR). Each predicate that is added to the group will be joined with the specified operator. Multiple predicate groups can be joined together since each predicate group implements IPredicate. In the example below, we create a predicate group with an AND operator: Code using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<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 SQL SELECT
[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 IPredicate, you can chain them together to create complex compound predicates. In the example below, we create two predicate groups and then join them together with a third predicate group: Code using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var pgMain = new PredicateGroup { Operator = GroupOperator.Or, Predicates = new List<IPredicate>() }; var pga = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<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); var pgb = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<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 SQL SELECT
[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)))
PropertyPredicate TODO Exists Predicate var subPred = Predicates.Field<User>(u => u.Email, Operator.Eq, "someone@somewhere.com");
var existsPred = Predicates.Exists<User>(subPred);
var existingUser = cn.GetList<User>(existsPred , null, tran).FirstOrDefault();

dapper extensions (predicates)的更多相关文章

  1. Dapper Extensions Change Schema

    Dapper Extensions Change Schema You can use the AutoClassMapper to assign a new schema to your model ...

  2. .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类

    引言 由公司需要使用dapper  同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions  并配套 生成实体类小工具的方 ...

  3. Dapper Extensions中修改Dialect

    如果是MySql数据库,则修改为:DapperExtensions.DapperExtensions.SqlDialect = new MySqlDialect(); DapperExtensions ...

  4. Dapper优秀资料

    dapper extensions (predicates) https://www.cnblogs.com/starluck/p/4542370.html

  5. Stackoverflow/dapper的Dapper-Extensions用法(二)

    之前翻译了Dapper-Extensions项目首页的readme.md,大家应该对这个类库的使用有一些了解了吧,接下来是wiki的文档翻译,主要提到了AutoClassMapper.KeyTypes ...

  6. Stackoverflow/dapper的Dapper-Extensions用法(一)

    Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...

  7. dapper的Dapper-Extensions用法(一)

    dapper的Dapper-Extensions用法(一) Dapper-Extensions Dapper Extensions is a small library that complement ...

  8. csharp:Dapper Sample

    You can find Dapper on Google Code here: http://code.google.com/p/dapper-dot-net/ and the GitHub dis ...

  9. asp.net core系列 66 Dapper介绍--Micro-ORM

    一.概述 目前对于.net的数据访问ORM工具很多,EF和EF Core是一个重量级的框架.最近在搭建新的项目架构,来学习一下轻量级的数据访问ORM工具Dapper.Dapper支持SQL Serve ...

随机推荐

  1. Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  2. cocos2d-x混合BlendFunc的使用

    1.什么是混合模式 “混合”是指两种颜色的叠加方式.在新图片将要渲染画到屏幕上的时候,将用在新图片中的红.绿.蓝和透明度信息,与屏幕上已经存在的图片颜色信息相融合. 说的具体一点,就是把某一像素位置上 ...

  3. python(7)– 类的反射

    python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员.获取成员.设置成员.删除成员. ...

  4. QP:price list 安全性设置

    1.添加职责 ORACLE PRICING ADMINISTRATOR 职责 (定价系统管理员) 2.Oracle Pricing Administrator Setup > Security ...

  5. Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。

    首先需要说明的是每个应用程序都是一个window,背景色为黑色.在window上可以跑多个view进行来回切换,下面就通过手动写代码来体现导航栏切换view的原理. 第一步,新建一个single vi ...

  6. js常用代码收藏

    --1.遍历string分割为数组 <script language="javascript"> str="2,2,3,5,6,6"; //这是一字 ...

  7. hdu 4421 2-SAT问题

    思路:我们需要判断是否有满足的a[n],其实也就是对每一个二进制位进行判断,看是否有满足的.那么我们每次取出一个二进制位,这样每一位只有0,1两种状态,就成了比较典型的2-SAT问题了. #inclu ...

  8. 纯CSS3制作进度条源代码

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  9. JMS - 基本概念

    连接工厂创建连接对象的工厂. 连接客户端与 JMS 服务器之间建立的连接.创建一个或多个会话. 会话创建消息.生产者和消费者,会话是 消息由三部分组成:消息头.消息属性和消息体. 生产者创建和发送消息 ...

  10. UML学习-总体概念篇

    前言:我们在实施一个项目时,前期的设计是非常重要的,如建筑师在建造一个建筑时,需要事先设计图纸,设计图纸是设计的语言,是不同的工程设计人员.设计人员和生产人员之间进行沟通的语言,在一个现代化的工程里面 ...