Solved

executing the right LINQ

Posted on 2011-09-06
8
289 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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