我正在通过 linq to sql 模拟链接服务器上的连接。我的问题是,linqtosql 似乎将 y.Xstatuses 的所有行都带入内存,然后进行连接。如果这是真的,我如何将所有内存保留在 sql server 上(并且仍然进行跨数据上下文连接操作),如果这不是真的,那会发生什么正在吃掉我所有的 ram?
var x = new fooDataContext();
var y = new barDataContext();
var allXNotDeleted = (from w in x.CoolTable
where x.IsDeleted != false).ToList();//for our demo this returns 218 records
var allXWithCompleteStatus = (from notDeleted in allXNotDeleted
join s in y.XStatuses on notDeleted.StatusID equals s.StatusID
where s.StatusID == 1
select notDeleted).Tolist();// insert massive memory gobbler here
return allXwithCompleteStatus;
编辑:尝试实现 Kevinbabcock 的想法
using (x = new fooDataContext())
using (var y = new barDataContext())
{
var n = (from notDeleted in x.GetTable<CoolTable>()
join z in y.GetTable<Xstatus>() on x.StatusID equals z.StatusID
where z.StatusID == 1 and x.IsDeleted != false
select x).ToList();
}
这仍然会引发跨上下文查询异常