Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > LINQ
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
LINQ Discuss Microsoft's LINQ (Language INtegrated Query) for .NET 3.0 and later.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the LINQ section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old December 30th, 2008, 06:18 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

No, no...I think what you show there is perfect.

You *ARE* using a DataReader.

The actual form of the query is almost surely nearly insignificant in terms of performance. And I never argued that LINQ is inherently inefficient. So long as the end result is that SQL Server *can* compile the query into its usually very very efficient final form, that actual compilation process is surely lost in the "noise" when compared with the process of reading tons of records and then transferring same to the .NET client.

Only reason for my comment was Eramgarden's statement that "We looked into typed data sets and compared to L2S, L2S was much better." Typed data sets--and ANY kind of datasets and datatables and datarows--are what I was commenting on. THOSE are what add all the overhead! If nothing else, consider that *ALL* the data returned by the query must be processed and stuffed into in-memory structures. Compare that to a datareader where a "smart" user can pick and choose the data to use. And where the in-memory cost *can* be significantly smaller. That's all. Nothing to do with the query per se or the DB server processing.
  #12 (permalink)  
Old December 30th, 2008, 06:26 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
Originally Posted by Old Pedant View Post
And I never argued that LINQ is inherently inefficient.
No you didn't but, depending on the situation, I would argue that it can be inefficient.

I further agree on your statement about DataReader's they are, after all, fire hoses when it comes to returning data from the database. Though I would argue that if the "smart" users are able to pick and choose what they want out of a resultset, they should be smart enough to simply write a query that returns them said resultset and not worry about the additional overhead incurred by returning columns of data that will never be used! ;]


Good stuff.

-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #13 (permalink)  
Old December 30th, 2008, 11:09 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, one of my own favorite "tricks" is creating category/subcategory displays by using a single query.

So you do something like
Code:
SELECT cat.x, cat.y, cat.z, subcat.a, subcat.b,...
FROM cat LEFT JOIN subcat ON cat.catid = subcat.catid
ORDER BY cat.x, subcat.a
And of course all you do, as you traipse through the datareaders records, is look for a change in cat.x and output a category heading when that occurs.

But that means that the other cat.* fields are extraneous (when cat.x doesn't change).

So far as I know, this is the most efficient means of producing such a cat/subcat display. And it's made more efficient by ignoring all of the cat.* fields except cat.x for most records.

That's just one example, of course.

Yes, generally I agree with you. Make the SQL query as smart as possible to return as little data as possible. But there are at least a few exceptions, where returning a bit more data makes the client-side processing much more efficient.

[And yes, I do know how to do the equivalent using two datareaders, and of course that does work with SQL Server though not with other DBs...unless you open up separate connections for each reader, of course.]
  #14 (permalink)  
Old December 30th, 2008, 11:28 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I was never much a fan of opening separate Datareaders to achieve *anything*. Regardless of how clean the code was it always looked messy to me. *shrug*

Now, depending on the situation, you *could* use the XML Data Type to build an XML string on the fly and pass that back up for processing. Obviously, this is much more efficient if whatever is consuming your data can handle an XML String and transform it accordingly.

Case in point, the Menu control that we used on McCain's website was Telerik's r.a.d. menu control which has the LoadXML() method. By building an XML string on the fly from a given proc we were able to pass it directly to the nav control which then parsed it into the Cat/SubCat scenario that you described.

Send 12 developers into a room alone and ask them to build widget XYZ and you will wind up with widget XYZ being wrote 12 different ways. ;]

-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #15 (permalink)  
Old December 31st, 2008, 12:48 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

LOL! Boy do I agree on that! My favorite self-quote is this one:
Quote:
The answer is the same as the usual answer to most programming questions:
"It depends..."
  #16 (permalink)  
Old December 31st, 2008, 12:58 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

LOL, so true.
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #17 (permalink)  
Old December 31st, 2008, 01:00 PM
Authorized User
 
Join Date: Dec 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks guys for the good discussion. Is this correct about LINQ:

Is it true that if I'm using stored proc with LINQ, I'm working with a disconnected mode because we're not using the Context?? but if using the ad-hoc SQL with LINQ, we're in a connected mode..
  #18 (permalink)  
Old December 31st, 2008, 03:17 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I don't understand what you mean. If i bring a sproc into the DBML file (lets call it usp_foo and assume that it is a non-query) the only way to access it is by doing something like:

csharp Code:
using(SomeContext ctx  = new SomeContext())
{
   ctx.usp_foo();
}

Furthermore, if we assume that the sproc actually queries the database and you need to update a column in every record, your code then looks like this:

csharp Code:
using(SomeContext ctx  = new SomeContext())
{
    foreach(TableName tbl in ctx.usp_foo())
    {
        tbl.SomeColumn = String.Empty;
    }
   ctx.SubmitChanges();
}

As you can see, you are still in a connected state. This has been on of L2S downfalls from the on set is that there is no easy to work with the data in a disconnected state. (That is not to say that it can't be done, it can, it is just a long way around for something that should be readiliy available).

So to answer your question, regardless of which route you go, you are always connected to your context.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #19 (permalink)  
Old December 31st, 2008, 05:22 PM
Authorized User
 
Join Date: Dec 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, let me think and will post back
  #20 (permalink)  
Old December 31st, 2008, 06:26 PM
Authorized User
 
Join Date: Dec 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

http://www.west-wind.com/weblog/posts/246222.aspx

first paragraph: "This doesn't mean that it makes persistent connections to the database, but means that the DataContext instance maintains state

So, DataContext never disconnects..not even when the resultset is retrieved?
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 AD HOC Testdriven.net tests? phsarjk BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 1 May 17th, 2007 04:12 PM
Calling an insert stored proc from a select stored dzitam SQL Language 10 April 2nd, 2007 12:39 PM
How to get value from stored proc busybee ASP.NET 1.0 and 1.1 Basics 4 April 2nd, 2006 01:06 AM
Ad Hoc Query Classes Bob Bedell Pro VB.NET 2002/2003 0 September 21st, 2003 01:32 AM



All times are GMT -4. The time now is 10:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.