![](/img/trans.png)
[英]How to join two tables from two different sql server databases in a simple but complete c# project?
[英]How to join two different databases' tables in C# with Linq?
我尝试在 c# 中加入两个不同的数据库表,但它给了我一个错误我该如何处理?
这是我的查询:
var list = (from h in db.database1.AsEnumerable()
join j in NV_DB.database2.AsEnumerable()
on h.Creation_Date equals j.Creation_Date
where j.Ship_Status == 3 && h.CustomerNo == CustomerNo
select new
{
shipName = h.ShipName,
creationDate = j.Creation_Date,
endingDate = j.Ending_Date
}
).ToList();
如果我这样做,它会给我 System.OverflowException 错误。 但是当我在 sql 中运行它时,它只给了我 30 条记录*
您需要删除°AsEnumerable`。 虽然它不运行 sql,但当您在它实际将整个表带入内存的地方使用它时,然后在查询的一部分执行工作
您的回答基本上是此处接受的答案中的第一条评论: 我是否误解了 LINQ to SQL .AsEnumerable()?
虽然 AsEnumerable 在调用时不评估查询,但它肯定会产生影响。 查询中进一步调用的任何内容都将使用 LINQ to objects 进行评估,因此您无法将其他元素组合到查询中(另一个 Where 或 OrderBy 或任何此类性质的元素),这些元素将成为 SQL 语句的一部分。
这里有深入的解释: https : //www.codeproject.com/Articles/732425/IEnumerable-Vs-IQueryable
在从数据库查询数据时,IEnumerable 在服务器端执行选择查询,在客户端加载内存中的数据,然后过滤数据。 因此做更多的工作并且变得缓慢。
在从数据库查询数据时,IQueryable 使用所有过滤器在服务器端执行选择查询。 因此做更少的工作并变成 fas
要对此进行调试,请开始将您的语句分成更小的步骤:
var list1 = db.database1.AsEnumerable().ToList();
var list2 = NV_DB.database2.AsEnumerable().ToList();
var joinResult = list1.Join(list2, // join list1 and list2
list1Row => list1Row.CreationDate, // from every row in list1 take the CreationDate
list2Row => list2Row.CreationDate, // from every row in list2 take the CreationDate
(list1Row, list2Row) => new // when they match, make one new object
{
// You only need the following properties:
ShipName = list1Item.ShipName,
CreationDate = list2Item.CreationDate,
EndingDate = list2Item.EndingDate,
ShipStatus = list2Item.ShipStatus,
CustomerNo = list1Item.CustomerNo,
})
.ToList();
var whereResult = joinResult
.Where(joinedRow => joinedRow.ShipStatus == 3
&& joinedRow.CustomerNo == customerNo)
.ToList();
var selectResult = whereResult.Select(whereResultRow => new
{
ShipName = whereResultRow.ShipName,
CreationDate = whereResultRow.CreationDate,
EndingDate = whereResultRow.Ending_Date,
})
.ToList();
这完全作为可枚举执行(在您的本地进程中,而不是由数据库管理系统)。 我的猜测是这运行顺利。
现在结合前几个语句:
var joinResult = db.database1.AsEnumerable()
.Join(NV_DB.database2.AsEnumerable(), // join list1 and list2
list1Row => list1Row.CreationDate, // from every row in list1 take the CreationDate
list2Row => list2Row.CreationDate, // from every row in list2 take the CreationDate
(list1Row, list2Row) => new // when they match, make one new object
{
// You only need the following properties:
ShipName = list1Item.ShipName,
CreationDate = list2Item.CreationDate,
EndingDate = list2Item.EndingDate,
ShipStatus = list2Item.ShipStatus,
CustomerNo = list1Item.CustomerNo,
})
.ToList();
当这有效时,添加 Where:
var whereResult = db.database1.AsEnumerable()
.Join(NV_DB.database2.AsEnumerable(), ...)
.Where(joinedRow => joinedRow.ShipStatus == 3
&& joinedRow.CustomerNo == customerNo)
.ToList();
等等。
使用您的调试器,您会在几分钟内找到问题(取决于您的编译时间)。 我的猜测是它在您的加入范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.