Click here to Skip to main content
15,906,567 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: Documenting Design Pin
K. Shaffer22-Jan-09 13:00
K. Shaffer22-Jan-09 13:00 
QuestionDatabase Queries Pin
CodingYoshi6-Jan-09 17:20
CodingYoshi6-Jan-09 17:20 
AnswerRe: Database Queries Pin
Wendelius7-Jan-09 2:29
mentorWendelius7-Jan-09 2:29 
GeneralRe: Database Queries Pin
CodingYoshi7-Jan-09 8:49
CodingYoshi7-Jan-09 8:49 
GeneralRe: Database Queries Pin
Wendelius7-Jan-09 9:16
mentorWendelius7-Jan-09 9:16 
GeneralRe: Database Queries Pin
Jon Rista7-Jan-09 12:20
Jon Rista7-Jan-09 12:20 
GeneralRe: Database Queries Pin
Wendelius8-Jan-09 2:47
mentorWendelius8-Jan-09 2:47 
GeneralRe: Database Queries [modified] Pin
Jon Rista8-Jan-09 4:55
Jon Rista8-Jan-09 4:55 
I suggested L2S for a few reasons. First, its really simple, and easy to get into. Second, if you use SQL Server, its amazingly efficient...the SQL generated is about as efficient as it gets, and often rivals or surpasses the performance of manually written queries. Third, its fully integrated into Microsofts development tools, which lets you learn how to use modern O/RM without having to learn low-level details like xml configuration formats and the like until you really need to. Fourth, L2S has complete support for .NET 3.5 LINQ. Your developers can query anything, from any data source...collections, xml, arrays, and the database (if you use L2S) with a single, unified query language. Thats huge. You may end up needing something more powerful to fulfill your mapping needs...NHibernate or Entity Framework would definitely cover those other scenarios if you needed them (NHibernate doesn't have a very rich LINQ scenario right now...its SQL generator doesn't generate nearly as efficient of queries as L2S...EF's query generator is something of an anomaly right now.)

(Yes, it will eventually be superceded by Entity Framework...but thats a ways off. EF v1.0 was kind of a dismal failure (IMO), and v2.0 needs a LOT of work. L2S won't just dissapear, either...nothing Microsoft has ever deprecated has actually ever been removed from the .NET framework...backwards compatability and all that. L2S in its current form will be around for a long time, and according to Microsoft, it will still be maintained and improved a bit, at least for a while.)

Regarding the efficiency when working with object graphs (trees, networks, cumulative calculations, etc.), that is where L2S truely shines. You would expect it to make several round trips to retrieve a root entity and several collections of child entities. L2S has a highly intelligent expression processor that translates your queries into the most efficient SQL possible. Retrieval of object graphs usually results in a single query to the database that returns a single flat result set containing everything for the entire graph (you can configure what is retrieved up front with DataLoadOptions on your DataContext, similar to a fetch strategy for NHibernate or a .Include() sequence in EF).

When you need to retrieve aggregated data (I read cumulative calculations as aggregations...correct me if I am wrong), L2S also shines. Even though ORM's are primarily meant to support entity retrieval and update...L2S has a unique ability to retrieve arbitrary data sets when you query your conceptual model. Assuming you have a conceptual model along the lines of:

Customer
Order
OrderLine
Product

You can query for customer information AND the aggregated count of how many orders they have in the last 6 months very easily with L2S:

var anonCustWithOrderCt = from c in db.Customers
                          where c.LastName = "Smith"
                          select new
                          {
                              c.CustomerID,
                              c.LastName,
                              c.FirstName,
                              OrdersInLastSixMonths = c.Orders.Count(o => o.OrderDate > DateTime.Now.AddMonths(-6))
                          }


The query generated will be very compact and efficient. COUNT in SQL Server is given special consideration by the query execution engine, so the following result is optimal:

SELECT [t0].[CustomerID], [t0].LastName, [t0].FirstName, (
    SELECT COUNT(*)
    FROM [Orders] AS [t1]
    WHERE ([t1].[OrderDate] > @p0) AND ([t1].[CustomerID] = [t0].[CustomerID])
    ) AS [OrdersInLastSixMonths]
FROM [Customers] AS [t0]
WHERE [t0].LastName = 'Smith'
-- @p0: Input DateTime (Size = 0; Prec = 0; Scale = 0)


This example is extremely simple compared to the kinds of complex queries LINQ can handle. It fully supports grouping, joining, etc. The main difference is that it is based off of a conceptual model rather than the physical database model...which, if you have a complex model or a model with odd entities, the L2S query generator may have some trouble generating the most optimal query (it may be forced to add extra joins when you wouldn't expect them, etc.).

If you want to experiment, check out LinqPAD. Its a free tool that lets you quickly connect to any SQL Server database and start running LINQ queries against it in a few minutes.

modified on Thursday, January 8, 2009 11:03 AM

GeneralRe: Database Queries Pin
Wendelius8-Jan-09 5:25
mentorWendelius8-Jan-09 5:25 
GeneralRe: Database Queries Pin
Jon Rista8-Jan-09 10:56
Jon Rista8-Jan-09 10:56 
GeneralRe: Database Queries Pin
Mark Churchill7-Jan-09 13:53
Mark Churchill7-Jan-09 13:53 
QuestionSoftware development Pin
systemexpertise1-Jan-09 18:26
systemexpertise1-Jan-09 18:26 
AnswerRe: Software development Pin
Cosmic Egg10-Jan-09 3:29
Cosmic Egg10-Jan-09 3:29 
QuestionStylesheet in VS2008 Pin
sarang_k30-Dec-08 19:01
sarang_k30-Dec-08 19:01 
AnswerRe: Stylesheet in VS2008 Pin
CodingYoshi4-Jan-09 15:45
CodingYoshi4-Jan-09 15:45 
QuestionException Hierarchy Pin
CodingYoshi29-Dec-08 11:27
CodingYoshi29-Dec-08 11:27 
GeneralRe: Exception Hierarchy Pin
Luc Pattyn29-Dec-08 15:12
sitebuilderLuc Pattyn29-Dec-08 15:12 
GeneralRe: Exception Hierarchy Pin
CodingYoshi30-Dec-08 3:18
CodingYoshi30-Dec-08 3:18 
AnswerRe: Exception Hierarchy Pin
Jon Rista6-Jan-09 8:48
Jon Rista6-Jan-09 8:48 
GeneralRe: Exception Hierarchy Pin
Leftyfarrell6-Jan-09 13:29
Leftyfarrell6-Jan-09 13:29 
GeneralRe: Exception Hierarchy Pin
Jon Rista6-Jan-09 17:31
Jon Rista6-Jan-09 17:31 
GeneralRe: Exception Hierarchy Pin
Curtis Schlak.25-Feb-09 9:02
Curtis Schlak.25-Feb-09 9:02 
AnswerRe: Exception Hierarchy Pin
Curtis Schlak.25-Feb-09 9:20
Curtis Schlak.25-Feb-09 9:20 
QuestionWho can show me the process and method of Scrum? Pin
Tal Rasha's Guardianship18-Dec-08 21:32
Tal Rasha's Guardianship18-Dec-08 21:32 
AnswerRe: Who can show me the process and method of Scrum? Pin
Ashfield18-Dec-08 22:01
Ashfield18-Dec-08 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.