I am fairly new to linq and would appreciate some help with the following select query which is throwing an out of memory exception (i have 4GB).
The same query returns a single row of data in less than a second when run is server explorer using visual studio 2008. PC_ID is the auto increment primary key in the table which has approx 80,000 rows of data.
I accept that there are a lot of columns but many are null and it's only one row so i am not sure why i should be getting an out of memory message.
Perhaps i should point out that the query works if i cut the select statement in half but then i don't have all the data that i need to build the page. I am running .net 3.5
Is there is a better way of doing this ?
Thanks in advance.
Richard
using (MyDatabaseDataContext db = new MyDatabaseDataContext()) {
var myProductTitleResults = (from pc1 in db.vwProductCatalogues
My guess is that behind the scenes, when newing up the object you are probably looking all objects into memory before selecting the first one.
Why don't you just select pc1 instead of specifying all the columns? You could just write
using (MyDatabaseDataContext db = new MyDatabaseDataContext()) {
var myProductTitleResults = (from pc1 in db.vwProductCatalogues
where pc1.PC_ID.Equals(CatID)
select pc1
}).FirstOrDefault();
if (myProductTitleResults != null) { // some code }
Marked as answer by pennyblack on Feb 24, 2013 11:04 PM
pennyblack
Member
1 Points
4 Posts
LinQ Query - Out of Memory Exception
Feb 24, 2013 09:55 PM|LINK
I am fairly new to linq and would appreciate some help with the following select query which is throwing an out of memory exception (i have 4GB).
The same query returns a single row of data in less than a second when run is server explorer using visual studio 2008. PC_ID is the auto increment primary key in the table which has approx 80,000 rows of data.
I accept that there are a lot of columns but many are null and it's only one row so i am not sure why i should be getting an out of memory message.
Perhaps i should point out that the query works if i cut the select statement in half but then i don't have all the data that i need to build the page. I am running .net 3.5
Is there is a better way of doing this ?
Thanks in advance.
Richard
using (MyDatabaseDataContext db = new MyDatabaseDataContext()) {
var myProductTitleResults = (from pc1 in db.vwProductCatalogues
where pc1.PC_ID.Equals(CatID)
select new {
pc1.LI_LabelName, pc1.PF_Description, pc1.BI_BandName, pc1.PC_Title, pc1.PC_ID, pc1.BI_ID, pc1.LI_ID, pc1.PC_CatalogueNumber, pc1.MC_Description, pc1.PC_OtherInfo, pc1.PC_FeaturedBands, pc1.PC_ReleaseNotes, pc1.PC_PressRelease, pc1.PC_ReleaseYear, pc1.PC_T1, pc1.PC_T2, pc1.PC_T3, pc1.PC_T4, pc1.PC_T5, pc1.PC_T6, pc1.PC_T7, pc1.PC_T8, pc1.PC_T9, pc1.PC_T10, pc1.PC_T11, pc1.PC_T12, pc1.PC_T13, pc1.PC_T14, pc1.PC_T15, pc1.PC_T16, pc1.PC_T17, pc1.PC_T18, pc1.PC_T19, pc1.PC_T20, pc1.PC_T21, pc1.PC_T22, pc1.PC_T23, pc1.PC_T24, pc1.PC_T25, pc1.PC_T26, pc1.PC_T27, pc1.PC_T28, pc1.PC_T29, pc1.PC_T30, pc1.PC_T31, pc1.PC_T32, pc1.PC_T33, pc1.PC_T34, pc1.PC_T35, pc1.PC_T36, pc1.PC_T37, pc1.PC_T38, pc1.PC_T39, pc1.PC_T40, pc1.PC_T41, pc1.PC_T42, pc1.PC_T43, pc1.PC_T44, pc1.PC_T45, pc1.PC_T46, pc1.PC_T47, pc1.PC_T48, pc1.PC_T49, pc1.PC_T50, pc1.PC_SleeveLocation, pc1.PC_SleeveFile, pc1.BI_RelatedBands, pc1.BI_RelatedBandsTemp, pc1.BI_BandAlias, pc1.PC_Country, pc1.PC_VT1, pc1.PC_VT2, pc1.PC_VT3, pc1.PC_VT4, pc1.PC_VT5, pc1.PC_VT6, pc1.PC_VT7, pc1.PC_VT8, pc1.PC_VT9, pc1.PC_VT10, pc1.PC_VT11, pc1.PC_VT12, pc1.PC_VT13, pc1.PC_VT14, pc1.PC_VT15, pc1.PC_VT16, pc1.PC_VT17, pc1.PC_VT18, pc1.PC_VT19, pc1.PC_VT20, pc1.PC_VT21, pc1.PC_VT22, pc1.PC_VT23, pc1.PC_VT24, pc1.PC_VT25, pc1.PC_VT26, pc1.PC_VT27, pc1.PC_VT28, pc1.PC_VT29, pc1.PC_VT30, pc1.PC_VT31, pc1.PC_VT32, pc1.PC_VT33, pc1.PC_VT34, pc1.PC_VT35, pc1.PC_VT36, pc1.PC_VT37, pc1.PC_VT38, pc1.PC_VT39, pc1.PC_VT40, pc1.PC_VT41, pc1.PC_VT42, pc1.PC_VT43, pc1.PC_VT44, pc1.PC_VT45, pc1.PC_VT46, pc1.PC_VT47, pc1.PC_VT48, pc1.PC_VT49, pc1.PC_VT50, pc1.PC_VB1, pc1.PC_VB2, pc1.PC_VB3, pc1.PC_VB4, pc1.PC_VB5, pc1.PC_VB6, pc1.PC_VB7, pc1.PC_VB8, pc1.PC_VB9, pc1.PC_VB10, pc1.PC_VB11, pc1.PC_VB12, pc1.PC_VB13, pc1.PC_VB14, pc1.PC_VB15, pc1.PC_VB16, pc1.PC_VB17, pc1.PC_VB18, pc1.PC_VB19, pc1.PC_VB20, pc1.PC_VB21, pc1.PC_VB22, pc1.PC_VB23, pc1.PC_VB24, pc1.PC_VB25, pc1.PC_VB26, pc1.PC_VB27, pc1.PC_VB28, pc1.PC_VB29, pc1.PC_VB30, pc1.PC_VB31, pc1.PC_VB32, pc1.PC_VB33, pc1.PC_VB34, pc1.PC_VB35, pc1.PC_VB36, pc1.PC_VB37, pc1.PC_VB38, pc1.PC_VB39, pc1.PC_VB40, pc1.PC_VB41, pc1.PC_VB42, pc1.PC_VB43, pc1.PC_VB44, pc1.PC_VB45, pc1.PC_VB46, pc1.PC_VB47, pc1.PC_VB48, pc1.PC_VB49, pc1.PC_VB50, pc1.PC_VTA1, pc1.PC_VTA2, pc1.PC_VTA3, pc1.PC_VTA4, pc1.PC_VTA5, pc1.PC_VTA6, pc1.PC_VTA7, pc1.PC_VTA8, pc1.PC_VTA9, pc1.PC_VTA10, pc1.PC_VTA11, pc1.PC_VTA12, pc1.PC_VTA13, pc1.PC_VTA14, pc1.PC_VTA15, pc1.PC_VTA16, pc1.PC_VTA17, pc1.PC_VTA18, pc1.PC_VTA19, pc1.PC_VTA20, pc1.PC_VTA21, pc1.PC_VTA22, pc1.PC_VTA23, pc1.PC_VTA24, pc1.PC_VTA25, pc1.PC_VTA26, pc1.PC_VTA27, pc1.PC_VTA28, pc1.PC_VTA29, pc1.PC_VTA30, pc1.PC_VTA31, pc1.PC_VTA32, pc1.PC_VTA33, pc1.PC_VTA34, pc1.PC_VTA35, pc1.PC_VTA36, pc1.PC_VTA37, pc1.PC_VTA38, pc1.PC_VTA39, pc1.PC_VTA40, pc1.PC_VTA41, pc1.PC_VTA42, pc1.PC_VTA43, pc1.PC_VTA44, pc1.PC_VTA45, pc1.PC_VTA46, pc1.PC_VTA47, pc1.PC_VTA48, pc1.PC_VTA49, pc1.PC_VTA50, pc1.PF_ID, pc1.PS_ID,
}).FirstOrDefault();
if (myProductTitleResults != null) { // some code }
dahla
Participant
1816 Points
369 Posts
Re: LinQ Query - Out of Memory Exception
Feb 24, 2013 10:38 PM|LINK
My guess is that behind the scenes, when newing up the object you are probably looking all objects into memory before selecting the first one.
Why don't you just select pc1 instead of specifying all the columns? You could just write
using (MyDatabaseDataContext db = new MyDatabaseDataContext()) { var myProductTitleResults = (from pc1 in db.vwProductCatalogues where pc1.PC_ID.Equals(CatID) select pc1 }).FirstOrDefault(); if (myProductTitleResults != null) { // some code }pennyblack
Member
1 Points
4 Posts
Re: LinQ Query - Out of Memory Exception
Feb 24, 2013 11:08 PM|LINK
Fantastic, I guessed it might be uploading the whole table into memory before performing the select but wasn't sure how to fix the problem.
Anyhow, your simple fix solved the problem and considerably reduced the amount of code at the same time. Much appreciated, Many Thanks,
Richard