How do i increase the performance of below linq query?
如何提高以下linq查询的性能?
While running it, it threw an error of System.OutOfMemoryException
. Note: I have a lot of records in XrmContext.sun_POSSet
entity
在运行它时,它抛出了System.OutOfMemoryException的错误。注意:我在XrmContext.sun_POSSet实体中有很多记录
var a = (from temple in XrmContext.sun_POSSet
select new POS()
{
id = temple.Id,
Country = temple.sun_sun_country_sun_POS == null ? "" : temple.sun_sun_country_sun_POS.sun_name,
CountryId = temple.sun_sun_country_sun_POS == null ? "" : temple.sun_sun_country_sun_POS.Id.ToString(),
stringint = temple.sun_sun_stringint_sun_placeofstu == null ? "" : temple.sun_sun_stringint_sun_placeofstu.sun_name,
stringintId = temple.sun_sun_stringint_sun_placeofstu == null ? "" : temple.sun_sun_stringint_sun_placeofstu.Id.ToString(),
FullName = temple.sun_contact_sun_POS.FullName,
EMail = temple.sun_contact_sun_POS.EMailAddress1,
MobilePhone = temple.sun_contact_sun_POS.MobilePhone
}).ToList<POS>();
1 个解决方案
#1
3
Without further information about the number of records, I'd say you're getting an OutOfMemoryException
because you are calling ToList()
on a dataset that is too large to be held in memory.
如果没有关于记录数量的进一步信息,我会说你得到一个OutOfMemoryException,因为你在一个太大而不能保存在内存中的数据集上调用ToList()。
ToList()
forces evaluation of the underlying IQueryable<T>
, which in this case results in all records being returned to the client and instantiated in memory. In your case, there simply isn't enough room - and you shouldn't assume that there will be. Make your datasets small when you bring them back to the client.
ToList()强制评估基础IQueryable
You should consider using Skip()
and Take()
to implement paging, or using an appropriate Where()
clause to filter the data.
您应该考虑使用Skip()和Take()来实现分页,或者使用适当的Where()子句来过滤数据。
#1
3
Without further information about the number of records, I'd say you're getting an OutOfMemoryException
because you are calling ToList()
on a dataset that is too large to be held in memory.
如果没有关于记录数量的进一步信息,我会说你得到一个OutOfMemoryException,因为你在一个太大而不能保存在内存中的数据集上调用ToList()。
ToList()
forces evaluation of the underlying IQueryable<T>
, which in this case results in all records being returned to the client and instantiated in memory. In your case, there simply isn't enough room - and you shouldn't assume that there will be. Make your datasets small when you bring them back to the client.
ToList()强制评估基础IQueryable
You should consider using Skip()
and Take()
to implement paging, or using an appropriate Where()
clause to filter the data.
您应该考虑使用Skip()和Take()来实现分页,或者使用适当的Where()子句来过滤数据。