Tech Tock

Time is of the essence.

VS 2010 – 5 – 10 Years

Visual Studio 2010 can compare SQL Server Databases.  Of course my software from 2000, and RedGate from slightly later could do that too.  Mine could also compare data…

While I still love my platform and think VS 2010 is awesome, if you’re looking for utilities, what you’ll see in 2015 is probably already out there today.

October 6, 2010 Posted by | Uncategorized | , , | 1 Comment

LINQ Group By With Multiple Aggregates

It appears that LINQ does not support a query that outputs multiple group by aggregate fields.  For example, there’s Min() and a Max() functions, so to get both min & max from a list, you seem to need to make 2 LINQ queries:

 var min = (from d in data select d).Min();
 var max = (from d in data select d).Max();

In SQL, the 2 values would pop out easily from a single query:

 Select min(d), max(d) from data

LINQ has all the power of SQL and works similarly, just the syntax is different.  To achieve this result we have to use a group by.  But we’ll be grouping by nothing, including all items in the set in a single bucket. I usually group by “1”, which makes a single grouping of all the elements (grouping by any single object will do the same):

 var minMax = from d in values
    group d by 1 into g
    select new { Min = g.Min(x => x),
     Max = g.Max(x => x) };

 

minMax.First() now contains the min and max for the set of values.

November 18, 2009 Posted by | Uncategorized | , , , , , , , , , | 7 Comments