Solved

executing the right LINQ

Posted on 2011-09-06
8
285 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 74

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
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 36492650
@kaufmed,

>>What is the type of myArticleOverrides?

The type of myArticleOverrides is List(Of ArticleOverride)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 74

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 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now