Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dapper.NET-映射多对多数据库关系?_C#_Sql Server_Dapper - Fatal编程技术网

C# Dapper.NET-映射多对多数据库关系?

C# Dapper.NET-映射多对多数据库关系?,c#,sql-server,dapper,C#,Sql Server,Dapper,我在SQL Server中有两个表,如下所示: Table1 int Id varchar(32) Name varchar(256) Description Table2 int LeftKey int RightKey select lk.*, rk.* from Table2 t2 inner join Table1 t1_left on t1_left.Id = t2.LeftKey inner join Table1 t1_right on

我在SQL Server中有两个表,如下所示:

Table1
    int Id
    varchar(32) Name
    varchar(256) Description

Table2
    int LeftKey
    int RightKey
select 
lk.*,
rk.*
from Table2 t2
inner join Table1 t1_left on t1_left.Id = t2.LeftKey
inner join Table1 t1_right on t1_right.Id = t2.RightKey
connection.Query<MyClass,MyClass,Match>(sqlhere,(mc1,mc2)=>{

var match = new Match();
match.Left = mc1;
match.Right = mc2;

return match;

});
我在C应用程序中有两个类,如:

MyClass
    Name
    Description

Match
    MyClass Left
    MyClass Right
我要做的是创建一个简洁的查询,它将选择表2中的所有行,连接到表1*键->Id,并生成一个具有相应的左右属性集的匹配对象列表

有没有一种干净整洁的方法来处理这个问题


我已经研究了关于Stackoverflow的其他多对多问题,它们似乎都与我的场景不同

Dapper实际上只是一种拆分网格并快速为对象指定列的方法。听起来就像是同一个物体有两次——一次在左边,一次在右边

因此,您的sql可能如下所示:

Table1
    int Id
    varchar(32) Name
    varchar(256) Description

Table2
    int LeftKey
    int RightKey
select 
lk.*,
rk.*
from Table2 t2
inner join Table1 t1_left on t1_left.Id = t2.LeftKey
inner join Table1 t1_right on t1_right.Id = t2.RightKey
connection.Query<MyClass,MyClass,Match>(sqlhere,(mc1,mc2)=>{

var match = new Match();
match.Left = mc1;
match.Right = mc2;

return match;

});
你的衣冠楚楚可能是这样的:

Table1
    int Id
    varchar(32) Name
    varchar(256) Description

Table2
    int LeftKey
    int RightKey
select 
lk.*,
rk.*
from Table2 t2
inner join Table1 t1_left on t1_left.Id = t2.LeftKey
inner join Table1 t1_right on t1_right.Id = t2.RightKey
connection.Query<MyClass,MyClass,Match>(sqlhere,(mc1,mc2)=>{

var match = new Match();
match.Left = mc1;
match.Right = mc2;

return match;

});
不需要在sql中从表2中返回任何内容,也不需要使用Dapper自动映射它。它实际上就像一个元组,所以我们可以在return func中创建它