首先,这是针对遗留应用程序,因此我无法从根本上更改任何逻辑。
我有数据库,有两个没有任何关系的表。我问我是否改变这个,但被告知我不能。
这些表可以描述为
Create Table T1
[doc_id] [int] NOT NULL, -- Primary Key
[customer_acc_no] [varchar](16) NULL,
[gcompany] [varchar](30) NULL,
....
extra fields
和表格
Create Table T2
[UserURN] [int] NOT NULL, -- All three fields make up
[AccountNumber] [varchar](20) NOT NULL, -- the primary key
[Company] [varchar](50) NOT NULL,
....
extra fields
如您所见,不仅字段名称不同,而且它们的长度也不同。
我正在使用存储库和工作单元模式。到目前为止,我已经设法编写了以下内容:
private IRepository<T1> _t1Repository;
private IRepository<T2> _t2Repository;
这些填充在构造函数中。
接下来,我使用以下代码来配置获取可查询的存储库。
var retVal = _t1Repository.Queryable();
由此,我正在尝试添加以下连接。
from q in T1
join w in T2
on new { X1 = q.gcompany, X2 = q.Customer_acc_no }
equals new { X1 = w.Company, X2 = w.AccountNumber }
我认为它将是这样的:
var query = T1.GroupJoin(T2,
c => c.gcompany,
o => o.Company,
(c, result) => new Result(c.doc_id, result))
.GroupJoin(T2,
c => c.Customer_acc_no,
o => o.AccountNumber ,
(c, result) => new Result(c.doc_id, result));
但我不确定,因为到目前为止,所有尝试都以Visual Studio中的错误告终。
请参阅下面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication42
{
class Program
{
static void Main(string[] args)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("doc_id", typeof(int));
dt1.Columns.Add("customer_acc_no", typeof(string));
dt1.Columns.Add("gcompany", typeof(string));
dt1.Rows.Add(new object[] { 1, "100", "abc" });
dt1.Rows.Add(new object[] { 2, "100", "def" });
dt1.Rows.Add(new object[] { 3, "100", "def" });
dt1.Rows.Add(new object[] { 4, "101", "abc" });
dt1.Rows.Add(new object[] { 5, "101", "ghi" });
dt1.Rows.Add(new object[] { 6, "102", "jkl" });
dt1.Rows.Add(new object[] { 7, "102", "abc" });
dt1.Rows.Add(new object[] { 8, "102", "def" });
dt1.Rows.Add(new object[] { 9, "103", "abc" });
dt1.Rows.Add(new object[] { 10, "103", "abc" });
DataTable dt2 = new DataTable();
dt2.Columns.Add("UserURN", typeof(int));
dt2.Columns.Add("AccountNumber", typeof(string));
dt2.Columns.Add("Company", typeof(string));
dt2.Rows.Add(new object[] { 11, "100", "abc" });
dt2.Rows.Add(new object[] { 12, "100", "def" });
dt2.Rows.Add(new object[] { 13, "100", "def" });
dt2.Rows.Add(new object[] { 14, "101", "abc" });
dt2.Rows.Add(new object[] { 15, "101", "ghi" });
dt2.Rows.Add(new object[] { 16, "102", "jkl" });
dt2.Rows.Add(new object[] { 17, "102", "abc" });
dt2.Rows.Add(new object[] { 18, "102", "def" });
dt2.Rows.Add(new object[] { 19, "103", "abc" });
dt2.Rows.Add(new object[] { 20, "103", "abc" });
var results = from r1 in dt1.AsEnumerable()
join r2 in dt2.AsEnumerable() on
new { x1 = r1.Field<string>("customer_acc_no"), x2 = r1.Field<string>("gcompany") } equals
new { x1 = r2.Field<string>("AccountNumber"), x2 = r2.Field<string>("Company") }
select new { t1 = r1, t2 = r2 };
}
}
}