PagodNaUtak
asked on
executing the right LINQ
Which is the right approach? The first code or the second code?
Dim temp As List(Of ArticleOverride) = myArticleOverrides
myArticleOverrides = (From article In temp _
Select article _
Where article.Url IsNot Nothing _
AndAlso article.Url.Trim.Length > 0).ToList
myArticleOverrides = (From article In myArticleOverrides _
Select article _
Where article.Url IsNot Nothing _
AndAlso article.Url.Trim.Length > 0).ToList
What is the type of myArticleOverrides?
Scott Guthrey ( Microsoft ) wrote a LINQ helper that actually shows you the SQL of the LINQ that your generating... Take a look at it as i think it could help you much in the future:
http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
ASKER
@kaufmed,
>>What is the type of myArticleOverrides?
The type of myArticleOverrides is List(Of ArticleOverride)
>>What is the type of myArticleOverrides?
The type of myArticleOverrides is List(Of ArticleOverride)
Then there's not much of a difference between either approach. The only real difference is that you are accessing the data via a different reference in your first option, namely temp.
ASKER
A colleauge of mine inform me that there is "Access to modified closure" in the second approach. Is that so?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Remember, the definition of the Linq query is just that: a definition. The execution of the query actually occurs in the For Each loop. The "closure" of the query is the collection being evaluated in the query and if you try to update the iterator variable, you are effectively stepping over the bounds of the closure--that is, you are introducing a new element to the system that didn't exist when the query was defined.
You should manage an IQueryable result or use an intermediate storage of List type
Dim myStorage As List(Of ArticleOverride) = (From article In myArticleOverrides _
Select article _
Where article.Url IsNot Nothing _
AndAlso article.Url.Trim.Length > 0).ToList
OR
myArticleOverrides = From article In myArticleOverrides _
Select article _
Where article.Url IsNot Nothing _
AndAlso article.Url.Trim.Length > 0