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

时间:2023-12-16 18:01:32

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

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Dapper;
using System.Data.SqlClient;
using System.Data;
using DapperApplicationByIn.Model; namespace DapperApplicationByIn
{
class Program
{
static void Main(string[] args)
{
// 定义连接字符串
string conn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString; #region in查询
using (IDbConnection connection = new SqlConnection(conn))
{
var sql = "select * from Users where Email in @emails";
var result = connection.Query<User>(sql, new { emails = new string[] { "fqy@qq.com", "hyj@163.com" } });
result.AsList().ForEach(p =>
{
Console.WriteLine("Id:"+p.UserId+" UserName:"+p.UserName+" Email:"+p.Email+" Address:"+p.Address);
});
}
#endregion Console.ReadKey();
}
}
}

程序运行结果:

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

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