SQL Server 2008 Launch Event

With SQL Server 2008 just released, there is a lot of interest of what it offers. On Friday, September 12, you can hear about all the new features of SQL Server 2008 in a full day of free training brought to you by JSSUG, Idea Integration and Pragmatic Works. Each session will dive deeply into new features from a BI, DBA and developer perspective around SQL Server 2008. To resister go to http://www.sqlsaturday.com and select the SQL Server 2008 Launch Event.

I will be presenting at this event the following session:

– T-SQL Enhancements in SQL Server 2008

LINQ Mysteries: The Distinct Function

Recently I had to use the Distinct function introduced in LINQ. My surprise was that depending on where you put the Distinct clause you will receive different results.

Let us take the following example: Let dtAnswers be a DataTable that has two columns, named answer_value and answer_comment. What I was seeking as a result was to return the count of the different answer_value values from all the rows. So knowing how it is done in SQL, I’ve wrote the following LINQ query in code:

Dim nDifferentValues As Integer = _
(From answer in dtAnswers.Rows _
Distinct Select answer("answer_value").ToString()).Count()

My surprise was that this will return always the same thing, no matter what rows I have and what values I have in the answer_value column. So after struggling several hours I’ve decided to try the Function syntax of LINQ:

Dim nDifferentValues As Integer = _
(From answer in dtAnswers.Rows _
Select answer("answer_value").ToString()).Distinct.Count()

Now this returns results correctly.
My guess is that the first query computes distinct on the rows first (by reference) and after that from the result selects answer("answer_value").ToString(). While the second query first selects answer("answer_value").ToString() and after that computes distinct.
So be careful where you put you LINQ functions 😉

Code Camp

On August 23, 2008 is the Jax Code Camp IV in Jacksonville. A full day of coding and free training on different technologies: WCF, LINQ, Cold Fusion, Flex/Air, Silverlight, Ruby on Rails, .Net Compact Framework, SharePoint, SQL Server 2008, and more. To sign up and see the full schedule visit the Code Camp website at http://www.jaxcodecamp.com/.

I will be presenting three sessions:

– Advanced SQL Querying Techniques
– SQL Injection
– SQL Server 2008 for Developers