如何提高以下linq查詢的性能?如何提高此LINQ查詢的性能?
運行它時,它拋出了一個錯誤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>();
如果你在這個表中有很多記錄,你是否真的需要把它們全部拉回來?也許增加一個where子句只提取相關記錄? –
這是'IQueryable'存在的原因之一。在調用'ToList()'之前,'IQueryable'存儲查詢以及如何處理結果,但實際上並沒有處理它們。使用Skip()和Take()可以取出你實際需要的記錄。如果你需要處理它們,可以批量處理(比如說)500 – Basic
你也可以使用IEnumerable,然後你可以逐一處理每個項目。理解這一點非常重要,當你這樣做時,任何流的「倒回」都是非常不起作用的。這是許多人建議你爲每個Linq查詢添加'.ToList()'的常見原因。 – Aron