Re: linq to sql duplicate select statements

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Are Linq-SQL methods commutative
    ... No - in LINQ each step effectively works off the result of the ... linq query which has to meet a SQL query they have in mind. ... freedom a SQL statement gives them: the SQL statement is the one ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Are Linq-SQL methods commutative
    ... because to translate what is being done in LINQ to ... of the previous query. ... the intention of the linq query with SQL. ... get 2 different resultsets by specifying them in a different order, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DBMS and lisp, etc.
    ... Naively implemented with SQL, again for 10 ... (1 query for the initial orders, 1 query for each order for its ... soon as you upgrade to the SQL database. ... (eq (order-customer orderA) ...
    (comp.lang.lisp)
  • Re: Building expression trees to filter derived entity classes
    ... up the inheritance correctly in your LINQ to SQL mapping? ... you project the your LINQ to SQL entity objects onto some other POCO ... LINQ to SQL is still trying to process the entire query, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Populating a list -- table structure?
    ... I had made a report already and figured out about adding the ... your responce below, but thanks to your help with SQL, I was able to get the ... It takes a summary from a select query and gives the ... KitID, long integer ...
    (microsoft.public.access.forms)