Tolist() 需要很长时间才能转换,我该如何改进呢?
Tolist() takes so long to convert, how can I improve this?
我有以下代码,其中我使用 tolist 方法将我的数据从数据库转换为列表。我必须将整个数据转换为列表的原因是,我必须在我使用 where 和 lambda 语句之后执行搜索操作,我们需要列表。
有什么替代方案吗?
// This takes less than 2 seconds to execute
var wdata = (from s in db.VIEW_ADDED_LOT
select new LotModel
{
CREATION_DATE = s.CREATION_DATE,
LOT_NO_SPL = s.LOT_NO_SPL,
LOT_TYPE = s.LOT_TYPE,
ITEM = s.ITEM,
BUSINESS_UNIT = s.BUSINESS_UNIT,
INSPECTOR = s.INSPECTOR,
NCRNO = s.NCRNO,
BUILDING_NO = s.BUILDING_NO,
CELL = s.CELL,
NCR_DT = s.NCR_DT,
INVENTORY_ROUTER = s.INVENTORY_ROUTER,
DOC_ISSUE = s.DOC_ISSUE,
COMMENTS = s.COMMENTS,
AGING = s.AGING,
ARCHIVAL_DATE = s.ARCHIVAL_DATE,
NCR_COMPLETION_STATUS = s.NCR_COMPLETION_STATUS,
FLAG_LINK = s.FLAG_LINK,
P_KEY = s.P_KEY
});
// This takes around 1 minute to convert to list as there is 500 000 rows
var data = wdata.ToList();
// The reason why I am converting to list is that I have to perform n number of
// search on the basis of the filter chosen by user
if (NCR != null && NCR != "")
{
data = data.Where(a => a.NCRNO == NCR).ToList();
}
if (LOT != null && LOT != "")
{
data = data.Where(a => a.LOT_NO_SPL == LOT).ToList();
}
在您的示例中,wdata.ToList()
调用评估查询并将整个 wdata
加载到内存中。您对 wdata
的初始分配仅创建一个 IQueryable
对象,它实际上并不查询数据库。
为避免性能下降,您应该将所有过滤器应用于 IQueryable
,然后在最后调用 ToList()
,例如:
var data = wdata; // at this point its a queryable of your initial linq
if (NCR != null && NCR != "")
{
data = data.Where(a => a.NCRNO == NCR); // appends one filter condition
}
if (LOT != null && LOT != "")
{
data = data.Where(a => a.LOT_NO_SPL == LOT); // appends another filter condition
}
var finalResult = data.ToList();
这会将您的条件附加到 IQueryable
,一旦您调用 .ToList()
,它最终将得到解决,这意味着您不必将所有实体加载到内存中,因为过滤器将在数据库中进行评估。
我有以下代码,其中我使用 tolist 方法将我的数据从数据库转换为列表。我必须将整个数据转换为列表的原因是,我必须在我使用 where 和 lambda 语句之后执行搜索操作,我们需要列表。
有什么替代方案吗?
// This takes less than 2 seconds to execute
var wdata = (from s in db.VIEW_ADDED_LOT
select new LotModel
{
CREATION_DATE = s.CREATION_DATE,
LOT_NO_SPL = s.LOT_NO_SPL,
LOT_TYPE = s.LOT_TYPE,
ITEM = s.ITEM,
BUSINESS_UNIT = s.BUSINESS_UNIT,
INSPECTOR = s.INSPECTOR,
NCRNO = s.NCRNO,
BUILDING_NO = s.BUILDING_NO,
CELL = s.CELL,
NCR_DT = s.NCR_DT,
INVENTORY_ROUTER = s.INVENTORY_ROUTER,
DOC_ISSUE = s.DOC_ISSUE,
COMMENTS = s.COMMENTS,
AGING = s.AGING,
ARCHIVAL_DATE = s.ARCHIVAL_DATE,
NCR_COMPLETION_STATUS = s.NCR_COMPLETION_STATUS,
FLAG_LINK = s.FLAG_LINK,
P_KEY = s.P_KEY
});
// This takes around 1 minute to convert to list as there is 500 000 rows
var data = wdata.ToList();
// The reason why I am converting to list is that I have to perform n number of
// search on the basis of the filter chosen by user
if (NCR != null && NCR != "")
{
data = data.Where(a => a.NCRNO == NCR).ToList();
}
if (LOT != null && LOT != "")
{
data = data.Where(a => a.LOT_NO_SPL == LOT).ToList();
}
在您的示例中,wdata.ToList()
调用评估查询并将整个 wdata
加载到内存中。您对 wdata
的初始分配仅创建一个 IQueryable
对象,它实际上并不查询数据库。
为避免性能下降,您应该将所有过滤器应用于 IQueryable
,然后在最后调用 ToList()
,例如:
var data = wdata; // at this point its a queryable of your initial linq
if (NCR != null && NCR != "")
{
data = data.Where(a => a.NCRNO == NCR); // appends one filter condition
}
if (LOT != null && LOT != "")
{
data = data.Where(a => a.LOT_NO_SPL == LOT); // appends another filter condition
}
var finalResult = data.ToList();
这会将您的条件附加到 IQueryable
,一旦您调用 .ToList()
,它最终将得到解决,这意味着您不必将所有实体加载到内存中,因为过滤器将在数据库中进行评估。