Re: linq to sql duplicate select statements
- From: bruce barker <nospam@xxxxxxxxxx>
- Date: Wed, 29 Oct 2008 22:38:03 -0700
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.LinqDataSourceStatusEventArgs) 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
- References:
- linq to sql duplicate select statements
- From: jbot
- linq to sql duplicate select statements
- Prev by Date: Re: Where to place class file
- Next by Date: Re: Sort GridView without DataSourceControl
- Previous by thread: linq to sql duplicate select statements
- Next by thread: Inheriting from RepeaterItem
- Index(es):
Relevant Pages
|