dapper extensions (predicates)
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)的更多相关文章
- Dapper Extensions Change Schema
Dapper Extensions Change Schema You can use the AutoClassMapper to assign a new schema to your model ...
- .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类
引言 由公司需要使用dapper 同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions 并配套 生成实体类小工具的方 ...
- Dapper Extensions中修改Dialect
如果是MySql数据库,则修改为:DapperExtensions.DapperExtensions.SqlDialect = new MySqlDialect(); DapperExtensions ...
- Dapper优秀资料
dapper extensions (predicates) https://www.cnblogs.com/starluck/p/4542370.html
- Stackoverflow/dapper的Dapper-Extensions用法(二)
之前翻译了Dapper-Extensions项目首页的readme.md,大家应该对这个类库的使用有一些了解了吧,接下来是wiki的文档翻译,主要提到了AutoClassMapper.KeyTypes ...
- Stackoverflow/dapper的Dapper-Extensions用法(一)
Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...
- dapper的Dapper-Extensions用法(一)
dapper的Dapper-Extensions用法(一) Dapper-Extensions Dapper Extensions is a small library that complement ...
- csharp:Dapper Sample
You can find Dapper on Google Code here: http://code.google.com/p/dapper-dot-net/ and the GitHub dis ...
- asp.net core系列 66 Dapper介绍--Micro-ORM
一.概述 目前对于.net的数据访问ORM工具很多,EF和EF Core是一个重量级的框架.最近在搭建新的项目架构,来学习一下轻量级的数据访问ORM工具Dapper.Dapper支持SQL Serve ...
随机推荐
- MDIO/MDC(SMI)接口
转载:http://blog.chinaunix.net/uid-24148050-id-132863.html 1. 简介 The MDIO interface is a simple, two-w ...
- Android(java)学习笔记73:线程组的概述和使用
Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. (1)默认情况下,所有的线程都属于主线程组. public final Thre ...
- FineUploader 学习笔记
FineUploader既是开源的又是收费的,这个没搞懂. 先看效果:
- Colossal Fibonacci Numbers(巨大的斐波那契数)UVA 11582
评测地址:http://acm.hust.edu.cn/vjudge/problem/41990 The i'th Fibonacci number f (i) is recursively de n ...
- Redis 命令 - Connection
AUTH password Authenticate to the server More: http://redis.io/commands/auth, http://www.redis.cn/co ...
- Jedis - hello world
Maven Dependency: <dependency> <groupId>redis.clients</groupId> <artifactId> ...
- android中Json数据保存方式
package com.example.savejsonproject; import java.io.File; import java.io.FileNotFoundException; impo ...
- 和阿文一起学H5——H5工具、素材
字体: 一般的H5工具都会提供一部分字体,如果提供的字体中没有自己想用的字体,可以在PPT或PS中进行加工,然后另存为图片,再导入到H5工具中去. 字全生成器: 1.http://www.diyizi ...
- Android获取屏幕宽度的4种方法
方法一: WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); int width = w ...
- windows下cmd命令行显示UTF8字符设置(CHCP命令)
本文由 www.169it.com 收集整理 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下 ...