?
Solved

executing the right LINQ

Posted on 2011-09-06
8
Medium Priority
?
295 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

864 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