轻量级ORM框架Dapper应用三:使用Dapper实现In操作

IN 操作符允许我们在 WHERE 子句中规定多个值。

本篇文章中,还是使用和上篇文章中同样的实体类和数据库,Dapper使用in操作符的代码如下:

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Configuration;
 7 using Dapper;
 8 using System.Data.SqlClient;
 9 using System.Data;
10 using DapperApplicationByIn.Model;
11 
12 namespace DapperApplicationByIn
13 {
14     class Program
15     {
16         static void Main(string[] args)
17         {
18             // 定义连接字符串
19             string conn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString;
20 
21             #region in查询
22             using (IDbConnection connection = new SqlConnection(conn))
23             {
24                 var sql = "select * from Users where Email in @emails";
25                 var result = connection.Query<User>(sql, new { emails = new string[2] { "fqy@qq.com", "hyj@163.com" } });
26                 result.AsList().ForEach(p => 
27                 {
28                     Console.WriteLine("Id:"+p.UserId+" UserName:"+p.UserName+" Email:"+p.Email+" Address:"+p.Address);
29                 });
30             }
31             #endregion
32 
33             Console.ReadKey();
34         }
35     }
36 }
复制代码

 程序运行结果:

示例代码下载地址:https://pan.baidu.com/s/1o7RokDs

posted @   .NET开发菜鸟  阅读(2222)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示