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, "[email protected]");
var existsPred = Predicates.Exists<User>(subPred);
var existingUser = cn.GetList<User>(existsPred , null, tran).FirstOrDefault();
时间: 2024-08-02 15:51:55

dapper extensions (predicates)的相关文章

Dapper Extensions Change Schema

Dapper Extensions Change Schema You can use the AutoClassMapper to assign a new schema to your model. An overview on this is on the extensions site. You will basically need to create an AutoClassMapper per model with a different schema. The best plac

DotNet 资源大全中文版(Awesome最新版)

Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台. 官网 ASP.NET WebAPI:快捷创建 HTTP 服务的框架,可以广泛用于多种不同的客户端,包括浏览器和移动设备. 官网 ServiceStack:架构缜密.速度飞快

DotNet 资源

DotNet 资源 目录 API 应用框架(Application Frameworks) 应用模板(Application Templates) 人工智能(Artificial Intelligence) 程序集处理(Assembly Manipulation) 资源(Assets) 认证和授权(Authentication and Authorization) 自动构建(Build Automation) 缓存(Caching) CLI CLR CMS 代码分析和度量(Code Analys

.Net开源框架列表

API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务的框架,可以广泛用于多种不同的客户端,包括浏览器和移动设备.官网 ServiceStack:架构缜密.速度飞快.令人愉悦的 web 服务.官网 Nelibur:Nelibur 是一个使用纯 WCF 构建的基于消息的 web 服务框架.Nelibur 可以便捷地创建高性能.基于消息的 web 服务,使

.Net 开源项目资源大全

Awesome DotNet,这又是一个 Awesome XXX 系列的资源整理,由 quozd 发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 伯乐在线已在 GitHub 上发起「DotNet 资源大全中文版」的整理.欢迎扩散.欢迎加入. https://github.com/jobbole/awesome-dotnet-cn (注:下面用 [$] 标注的表示收费工具,但部分收费工具针对开源软件的开发/部署/托管是免费的) API 框架

DotNet 资源大全中文版【转】

转自:https://github.com/jobbole/awesome-dotnet-cn 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-dotnet 是由 quozd 发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. Awesome 系列虽然挺全,但基本只对收录的资源做了极为简要的介绍,如果有更详细的中文介绍,对相应开发者的帮助会更大.这也是我们发起这个开源项目的初衷. (

DotNet 资源大全【转】

转自:http://blog.jobbole.com/96676/ API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台. ASP.NET WebAPI:快捷创建 HTTP 服务的框架,可以广泛用于多种不同的客户端,包括浏览器和移动设备. ServiceStack :架构缜密.速度飞快.令人愉悦的 web 服务. Nelibur:Nelibur 是一个使用纯 WCF 构建的基于消息的 web 服务框架.Neli

DotNet 资源大全

Awesome DotNet,这又是一个 Awesome XXX 系列的资源整理,由 quozd 发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. (注:下面用 [$] 标注的表示收费工具,但部分收费工具针对开源软件的开发/部署/托管是免费的) API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台. ASP.NET WebAPI:快捷创建 HTTP 服务的

《.NET开发资源大全》

目录 API 应用框架(Application Frameworks) 应用模板(Application Templates) 人工智能(Artificial Intelligence) 程序集处理(Assembly Manipulation) 资源(Assets) 认证和授权(Authentication and Authorization) 自动构建(Build Automation) 缓存(Caching) CLI CLR CMS 代码分析和度量(Code Analysis and Met