13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

322 CHAPTER 12 LINQ beyond collectionsFROM [dbo].[DefectUser] AS [t0]) AS [t1]ORDER BY [t1].[value]This is a good example of where the generated SQL is wordier than it needs to be.Although we couldn’t reference the elements of the f<strong>in</strong>al output sequence when perform<strong>in</strong>gan order<strong>in</strong>g on the query expression, you can <strong>in</strong> SQL. This simpler querywould have worked f<strong>in</strong>e:SELECT LEN([t0].[Name]) AS [value], [t0].[Name]FROM [dbo].[DefectUser] AS [t0]ORDER BY [value]Of course, what’s important is what the query optimizer does on the database—theexecution plan displayed <strong>in</strong> SQL Server Management Studio Express is the same forboth queries, so it doesn’t look like we’re los<strong>in</strong>g out.Next we’ll have a look at a couple of the jo<strong>in</strong>s we used <strong>in</strong> chapter 11.EXPLICIT JOINS: MATCHING DEFECTS WITH NOTIFICATION SUBSCRIPTIONSWe’ll try both <strong>in</strong>ner jo<strong>in</strong>s and group jo<strong>in</strong>s, us<strong>in</strong>g the examples of jo<strong>in</strong><strong>in</strong>g notificationsubscriptions aga<strong>in</strong>st projects. I suspect you’re used to the drill now—the pattern ofthe code is the same for each query, so from here on I’ll just show the query expressionand the generated SQL unless someth<strong>in</strong>g else is go<strong>in</strong>g on.// Query expression (modified from list<strong>in</strong>g 11.12)from defect <strong>in</strong> context.Defectsjo<strong>in</strong> subscription <strong>in</strong> context.NotificationSubscriptionson defect.Project equals subscription.Projectselect new { defect.Summary, subscription.EmailAddress }-- Generated SQLSELECT [t0].[Summary], [t1].[EmailAddress]FROM [dbo].[Defect] AS [t0]INNER JOIN [dbo].[NotificationSubscription] AS [t1]ON [t0].[ProjectID] = [t1].[ProjectID]Unsurpris<strong>in</strong>gly, it uses an <strong>in</strong>ner jo<strong>in</strong> <strong>in</strong> SQL. It would be easy to guess at the generatedSQL <strong>in</strong> this case. How about a group jo<strong>in</strong>, though? Well, this is where th<strong>in</strong>gs getslightly more hectic:// Query expression (modified from list<strong>in</strong>g 11.13)from defect <strong>in</strong> context.Defectsjo<strong>in</strong> subscription <strong>in</strong> context.NotificationSubscriptionson defect.Project equals subscription.Project<strong>in</strong>to groupedSubscriptionsselect new { Defect = defect, Subscriptions = groupedSubscriptions }-- Generated SQLSELECT [t0].[DefectID] AS [ID], [t0].[Created],[t0].[LastModified], [t0].[Summary], [t0].[Severity],[t0].[Status], [t0].[AssignedToUserID],[t0].[CreatedByUserID], [t0].[ProjectID],[t1].[NotificationSubscriptionID],[t1].[ProjectID] AS [ProjectID2], [t1].[EmailAddress],(SELECT COUNT(*)Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!