Linq output changes

So I have a pretty simple SQL call

SELECT count(col1) as count, sum(col2) as charges, col3, col4 
FROM table GROUP BY col3, col4 

When I do the above call I get 4,961 rows returned.

When I run the results through the following linq statement it returns the data in a different order every single time it runs.

I guess what my disconnect here is, is why do I get different results? Shouldn't the below linq block parse the data the same no matter what order it's in?

IEnumerable<Chart.Point> recs = from a in
   (from r in cache.Data
      where ids.Contains(r[idColName].ToString())
         group r by r[ranColName].ToString() into g
            select new Chart.Point { Key = g.Key, Value = g.Sum(x => 
                 Convert.ToInt64(x[countCol])) }).Take(numberOfColumns)
     orderby a.Value descending, a.Key
     select a;

Answers


Let's split the query to make things a little clearer:

var points = 
  from r in cache.Data                               
  where ids.Contains(r[idColName].ToString())       
  group r by r[ranColName].ToString() into g        
  select new Chart.Point {                          
   Key = g.Key,                                     
   Value = g.Sum(x => Convert.ToInt64(x[countCol])) 
  };

var topPoints = points.Take(numberOfColumns);

var orderedPoints = from a in topPoints
                    orderby a.Value descending, a.Key
                    select a;

The cache.Data property has an arbitrary order, because it's populated from a SQL query which doesn't specify ordering. This means that topPoints may end up with a different set of points each time. As a result, the ordering will appear to be different on different runs (but in reality it is the points being ordered which is different, not really the ordering of the points).

You should be able to get a consistent, ordered, and limited sequence of points by moving the Take operation after the ordering operation, so that the same points will always be at the top before the limit is applied.


Need Your Help

Using variable across forms

winforms variables

I have a string variable SuppCode which I assign in Form1. I want to use this variable in a Switch statement on Form2. I've tried using public string SuppCode; but I still get a The name 'Switch' d...

Why do Haskell type signature declarations have multiple arrows?

haskell syntax types

Sorry this is worded poorly, but it's hard to describe.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.