Linq Joins上的System.OutOfMemoryException

时间:2017-04-22 06:26:09

标签: c# entity-framework linq out-of-memory

我在下面有这个查询:

var data = (from ftr in db.TB_FTR
            join mst in db.TB_MST on ftr.MST_ID equals mst.MST_ID
            join trf in db.TB_TRF on mst.TRF_ID equals trf.ID
            select new TRFData 
            { 
                City = ftr.FTR_CITY, 
                County = ftr.FTR_COUNTY, 
                Type = trf.TRF_TYPE 
            }).ToList();

TB_FTR表包含3,000,000行,TB_MST包含1,100,000行,TB_TRF包含340行。如何使此加入工作?这有诀窍或解决方法吗?感谢。

1 个答案:

答案 0 :(得分:5)

Calling ToList you force the execution of the query and you ask all the data of the query to be loaded in memory in a List. Instead of following this approach, you could use either a streaming approach, not a bufferring one like you do, in which you would get one element at a time or even better you could follow a paging approach to get the first 100 records, then the next 100 records (on another request) and so on and so forth.

Streaming

var items = (from ftr in db.TB_FTR
             join mst in db.TB_MST on ftr.MST_ID equals mst.MST_ID
             join trf in db.TB_TRF on mst.TRF_ID equals trf.ID
             select new TRFData 
             { 
                 City = ftr.FTR_CITY, 
                 County = ftr.FTR_COUNTY, 
                 Type = trf.TRF_TYPE 
             });

foreach(var item in items)
{
    // ..
}

Paging

var pageSize = 100;
var pageNumber = 1;

var first100Items = items.Skip(pageSize*PageNumber)
                         .Take(paeSize)
                         .ToList();