在这篇文章中,讲解如何使用Dapper使用Inner join的操作
1、新创建两张表:Users表和Product表Users表定义如下:
CREATE TABLE [dbo].[Users](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](16) NULL,
[Email] [varchar](32) NULL,
[Address] [varchar](128) NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Product表定义如下:
CREATE TABLE [dbo].[Product](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[ProductName] [varchar](16) NULL,
[Price] [decimal](8, 2) NULL,
[UserId] [int] NULL,
PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
查看Users表和Product表会发现两张表通过UserId外键关联起来,然后我们需要在Product实体类上面进行修改,添加一个User的实体属性,修改后的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DapperApplicationJoin.Model
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public User UserOwner { get; set; }
public string Price { get; set; }
}
}
User实体类定义如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DapperApplicationJoin.Model
{
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
}
2、Main方法定义如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Dapper;
using DapperApplicationJoin.Model;
namespace DapperApplicationJoin
{
class Program
{
static void Main(string[] args)
{
string conn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString;
using (IDbConnection connection = new SqlConnection(conn))
{
string sql = @"
select p.ProductName,p.Price,u.UserName,u.UserId,u.Email,u.Address
from Product as p
join Users as u
on p.UserId=u.UserId;
";
var result = connection.Query<Product, User, Product>(sql,
(product, users) =>
{
product.UserOwner = users; return product;
}, splitOn: "UserName");
var query = connection.Query(sql);
// 输出 使用动态类型
query.AsList().ForEach(p =>
{
Console.WriteLine("产品名称:" + p.ProductName + ",产品价格:" + p.Price + ",用户姓名:" + p.UserName);
});
}
Console.ReadKey();
}
}
}
splitOn参数的含义:代码中的splitOn是UserName,运行时,会从查询结果所有字段列表的最后一个字段开始进行匹配,一直到找到UserName这个字段(大小写忽略无所谓),找到的第一个UserName字段匹配的Product类的UserName属性,那么从UserName到最后一个字段都属于Product,UserName以前的字段都被影射到Users,通过 (T, P) => {return T; },把两个类的实例解析出来。就实现了两个数据的解析,要不然dapper也不知道哪个是Product哪个是Users的属性。
除了可以使用上面的方法以外,还可以使用直接查询SQL语句的方式:
using (IDbConnection connection = new SqlConnection(conn))
{
string sql = @"
select p.ProductName,p.Price,u.UserName,u.UserId,u.Email,u.Address
from Product as p
join Users as u
on p.UserId=u.UserId;
";
var query = connection.Query(sql);
// 输出 使用动态类型
query.AsList().ForEach(p =>
{
Console.WriteLine("产品名称:" + p.ProductName + ",产品价格:" + p.Price + ",用户姓名:" + p.UserName);
});
}
运行结果如下:
示例代码下载地址:点此下载
到此这篇关于使用Dapper实现Join操作的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。