Querying with LINQ

Jul 24, 2012 at 4:17 AM
Edited Jul 24, 2012 at 4:43 AM

Hi Sebastian,

Thank you for your contribution to CodePlex. Netoffice is an excellent library. I am relatively new to C#, and I was wondering if you knew the answer to this question.

I like the syntax of LINQ and wanted to query the properties of an Access QueryDef as follows:

 

List<dao.Property> resultProps =
                (from dao.Property prop in myQueryDef.Properties.ToArray<dao.Property>()
                    where prop.Name == PROPERTY_NAME1 || prop.Name == PROPERTY_NAME2
                        select prop).ToList<dao.Property>();

 

Will doing this cause a memory leak because I'm not calling prop.Dispose() for each iteration? I could use a foreach statement, but I like the LINQ syntax and was hoping to use it.  I suspect it is a bad idea because I think that a dao.Property array is created with ToArray(), and even though only 2 items get returned in the list, there are no references to the rest of the array, and Dispose() cannot get called.

Jul 24, 2012 at 4:59 AM
Edited Jul 24, 2012 at 5:02 AM

I used a pretty simple workaround:

 

List<dao.Property> props = myQueryDef.Properties.ToList<dao.Property>();

List<dao.Property> resultProps =
	(from prop in props
	 where prop.Name == PROPERTY_NAME1 || prop.Name == PROPERTY_NAME2
	 select prop).ToList();

//... do something ...

props.ForEach(i => {i.Dispose(); });
resultProps.ForEach(i => { i.Dispose(); });

 

I assume I have to call Dispose() for all items in both lists-- is that right, or do I not need to re-call it for the 2 items that get returned from the LINQ?

Coordinator
Jul 24, 2012 at 7:17 AM

hello,

the easiest way is to use is:

myQueryDef.DisposeChildInstances();

these method dispose all created childs from myQueryDef. in this case Properties and all child proxies from Properties.
DisposeChildInstances() is implemented for all classes in NetOffice.

currently i try to create a more intelligent Linq support in NetOffice.

please take a look: http://download.codeplex.com/Download?ProjectName=netoffice&DownloadId=350995

the idea is an immediately dispose fot not matched items while execute ling statements. you can implement your own extension in this way as 'hot fix'

Sebastian




 

Jul 24, 2012 at 2:03 PM

That is great! I did not notice there was a DisposeChildInstances() method. I'll try that later and check out the download later when I am able. Thanks for your help and, again, great work!