Link to home
Start Free TrialLog in
Avatar of PagodNaUtak
PagodNaUtakFlag for Philippines

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

Open in new window

myArticleOverrides = (From article In myArticleOverrides _
                              Select article _
                              Where article.Url IsNot Nothing _
                              AndAlso article.Url.Trim.Length > 0).ToList

Open in new window

Avatar of Bardobrave
Bardobrave
Flag of Spain image

I think that what you are trying to do is not exactly possible, as you are trying to convert an IQueryable object (myArticleOverrides) to a List object.

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
Avatar of kaufmed
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
Avatar of PagodNaUtak

ASKER

@kaufmed,

>>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.
A colleauge of mine inform me that there is "Access to modified closure" in the second approach. Is that so?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.