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. iOS H5容器的一些探究(一):UIWebView和WKWebView的比较和选择

    一.Native开发中为什么需要H5容器 Native开发原生应用是手机操作系统厂商(目前主要是苹果的iOS和google的Android)对外界提供的标准化的开发模式,他们对于native开发提供了 ...

  2. Collections.synchronizedMap 详解

    众所周知,HashMap 本身非线程安全的,但是当使用 Collections.synchronizedMap(new HashMap()) 进行包装后就返回一个线程安全的Map. 怎么实现的呢?今天 ...

  3. Nodejs的mysql模块学习(一)

    介绍 mysql npm包 是一个nodejs的模块,由JavaScript编写 安装 npm install mysql 建立连接 var mysql = require('mysql');//引用 ...

  4. 实验教学管理系统 c语言程序代写源码下载

    问题描述:实验室基本业务活动包括:实验室.班级信息录入.查询与删除:实验室预定等.试设计一个实验教学管理系统,将上述业务活动借助计算机系统完成. 基本要求: 1.必须存储的信息 (1)实验室信息:编号 ...

  5. javaweb学习总结六(泛型)

    一:泛型的概念 泛型用来现在集合中的存储类型,防止取出时强制转换发生错误. 1:没有使用泛型时,如下: @Test public void test1() { List list = new Arra ...

  6. Java Concurrency - ThreadFactory, 使用工厂方法创建线程

    当需要创建多个类似的线程实例时,使用工厂模式替代 new 操作符创建线程,能使代码更为简洁,易于维护.JDK 提供了 java.util.concurrent.ThreadFactory 接口,Thr ...

  7. Android 命令行编译、打包生成apk文件

    一.搭建搭建环境 1. 安装JDK 和 Android SDK   2. 配置环境变量 D:\android-sdk-windows\tools C:\Program Files\Java\jdk1. ...

  8. C# Winform AutoScaleMode属性

    属性的用法: None : 禁用自动缩放.(默认时) Font :根据类使用的字体(通常为系统字体)的维度控制缩放. Dpi : 根据显示分辨率控制缩放.常用分辨率为 96 和 120 DPI. In ...

  9. Redis rdb文件CRC64校验算法 Java实现

    查看RDB文件结构,发现最后的8字节是CRC64校验算得,从文件头开始直到8字节校验码前的FF结束码(含),经过CRC64校验计算发现,貌似最后的8字节是小端模式实现的. 参考redis的crc64实 ...

  10. HTML+CSS学习笔记(3)- 认识标签(2)

    HTML+CSS学习笔记(3)- 认识标签(2) 1.使用ul,添加新闻信息列表 在浏览网页时,你会发现网页上有很多信息的列表,如新闻列表.图片列表, 这些列表就可以使用ul-li标签来完成.ul-l ...