zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    谈我们为什么学不好编程2——你是否已进入“等死模式”?
    JSP使用SmartUpload实现文件上传
    内核模块编程练习
    Vue++:Vue中 关于$emit的用法
    Vue++:Vue 脚手架在vue.config.js文件中配置scss全局变量
    Vue++:Vuecli3.0 脚手架搭建项目
    Java++:七个开源的 Spring Boot 前后端分离项目,一定要收藏!
    MySQL++:SQL 优化的15个小技巧
    RabbitMQ++:RabbitMQ 的队列(Queue)的参数及其含义
    Vue++:Vue 二级路由不显示页面问题
  • 原文地址:https://www.cnblogs.com/starluck/p/4542370.html
Copyright © 2011-2022 走看看