Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

executing the right LINQ

Posted on 2011-09-06
8
Medium Priority
?
293 Views
Last Modified: 2013-11-11
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

0
Comment
Question by:PagodNaUtak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 36488257
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36488493
What is the type of myArticleOverrides?
0
 
LVL 14

Expert Comment

by:binaryevo
ID: 36490153
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
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 8

Author Comment

by:PagodNaUtak
ID: 36492650
@kaufmed,

>>What is the type of myArticleOverrides?

The type of myArticleOverrides is List(Of ArticleOverride)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36493018
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.
0
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 36493380
A colleauge of mine inform me that there is "Access to modified closure" in the second approach. Is that so?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36497751
I've never encountered that particular error before, but in reading about it I find that you would have to be doing something like this to get the error:
myArticleOverrides = From article In myArticleOverrides _
                     Select article _
                     Where article.Url IsNot Nothing _
                     AndAlso article.Url.Trim.Length > 0
                     
For Each item in myArticleOverrides
    item = new Article()    ' Closure error here because you are trying to modify the iterator variable
Next

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36497790
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.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question