473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

linq to sql duplicate select statements

I've got some pages that use linq to sql. When I look at the
generated sql statements in sql server profiler, I see the same
statements repeated. In the example below (from a linq data source
selected statement), the same sql query is run against the database 3
times at:
oStudent.Count
oStudent(0).lkpstatus.studentstatus
oStudent(0).lkpstatus.statuscategory

Is this the expected behavior? I expected the select to be run only
once. Thanks in advance for help with this.

Private Sub lds_Student_Selected(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.LinqDataSourceStatusEven tArgs) Handles
lds_Student.Selected

Dim oStudent As List(Of personal) = CType(e.Result, List(Of
personal))
If oStudent.Count = 1 Then
Session("CurrentStudentStatus") =
oStudent(0).lkpstatus.studentstatus
Session("CurrentStudentStatusCategory") =
oStudent(0).lkpstatus.statuscategory
End If
End Sub

Jim
Oct 29 '08 #1
1 3333
unlike an IEnumerable linq query, which walks the objects to produce the
query collection (the walk is done at query time), the linq to sql
query result is an expression parse tree. the parse tree is converted to
sql and excuted when you iterate the query. so you never want to iterate
(foreach) the query results more than once.

if you need to foreach more than once (say multiple binding), convert
the query to a list or array, which can be reused without rerunning the
query.

-- bruce (sqlwork.com)

jbot wrote:
I've got some pages that use linq to sql. When I look at the
generated sql statements in sql server profiler, I see the same
statements repeated. In the example below (from a linq data source
selected statement), the same sql query is run against the database 3
times at:
oStudent.Count
oStudent(0).lkpstatus.studentstatus
oStudent(0).lkpstatus.statuscategory

Is this the expected behavior? I expected the select to be run only
once. Thanks in advance for help with this.

Private Sub lds_Student_Selected(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.LinqDataSourceStatusEven tArgs) Handles
lds_Student.Selected

Dim oStudent As List(Of personal) = CType(e.Result, List(Of
personal))
If oStudent.Count = 1 Then
Session("CurrentStudentStatus") =
oStudent(0).lkpstatus.studentstatus
Session("CurrentStudentStatusCategory") =
oStudent(0).lkpstatus.statuscategory
End If
End Sub

Jim
Oct 30 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
4
by: Arthur Dent | last post by:
Hello all, I am trying to figure out how to do something in LINQ, IF I can even do it in LINQ. Online samples and such for LINQ is still a bit on the sketchy side. Here is what I want to...
9
by: =?Utf-8?B?RXZlcnQ=?= | last post by:
In my (Windows Forms) project I am using strongly typed datatables. Now I am trying to convert my 'rowfilter/group by' query logic to Linq. Most queries I can easily convert, but some I am having...
6
by: Arjen | last post by:
Hi, My experience with linq is that I can develop my web application very fast. On the other hand, I have read that using stored procedures are executing faster. Is it smart to use linq for a...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: mesut | last post by:
Hi, I'm using LINQ.. and I would like to understand if it's a bug or not in LINQ... I've a situation like: I'm reading all records via "Select" Statement and a specific select statment #1...
21
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.