如何提高此LINQ查询的性能?

时间:2022-06-07 00:21:17

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()子句来过滤数据。