We have implemented a winforms application using vb.net. We are facing and verified some memory leaks using ANTS memory profiler.
The problem occurs when we have a LINQ with an if statement.
For example the below LINQ:
`Dim vLst = (From v In myPageDbContext.customer
Group Join p In myPageDbContext.discounts
On p.discountid Equals v.discountid Into Group From p In Group.DefaultIfEmpty
Where v.customerid = dCustId
Select firstname = If(v.firstname IsNot Nothing AndAlso v.firstname.Trim.Length > 0, v.firstname, ""),
surname = If(v.surname IsNot Nothing AndAlso v.surname.Trim.Length > 0, v.surname, v.dtitle),
v.phone1,
p.discpercent,
v.comments,
mobilephone = If(If(v.mobilephone Is Nothing, "", Trim(v.mobilephone)).Length = 0, If(v.phone2 Is Nothing, "", Trim(v.phone2)), If(v.mobilephone Is Nothing, "", Trim(v.mobilephone)))).FirstOrDefault()`
If we remove the if statement the problem is solved.
We have tried everything we no success and have found a solution by converting the LINQ with if statement to SQL commands that do not create a memory leak in our case.
Before doing the above we are just wondering if we are doing something wrong by using the if statement in LINQ and if this is normal?