Question: I wish to do a select and limit the number of future and previous events by 20 for example for the newest events first.

`future_events` and `previous_event` is a 1 or 0. I could store is as a single column if needed.

I think I'm missing a GROUP BY but my brains not with it this morning. This is what I have: 

```
SELECT name, start_timestamp, end_timestamp, future_event, previous_event, url
FROM events_table
WHERE status != 'draft' AND status != 'canceled'
-- AND SUM(previous_event) <= 20
-- AND SUM(future_event) <= 20
ORDER BY start_timestamp DESC

-- Sample Table
CREATE TABLE IF NOT EXISTS `events_table` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `url` varchar(500) NOT NULL,
  `start_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `end_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `status` varchar(10) NOT NULL,
  `future_event` tinyint(1) NOT NULL,
  `previous_event` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- Data
-- Each row is an event with a unique event, time, url etc.

```

**Expected Result**

* Returns max of 40 results in total
* Max 20 Future events (Where `future_event` = 1)
* Max 20 Previous events (Where `previous_event` = 1)
* Only shows the most recent events

Answer: 
Is this what you want?

```
var result = ret.ToDictionary
(
    r => r.Key, 
    v => v.Value.Select(n => (double)n/v.Value.Count(i => i != 0)).ToArray()
);

```

This will set the values to `NaN` for a row if all the elements of that row are zero. If instead you want to make the results for that row zero, you could change the code to:

```
var result = ret.ToDictionary
(
    r => r.Key, 
    v => v.Value.Select(n =>
    {
        double count = v.Value.Count(i => i != 0);
        return (count > 0) ? n/count : 0.0;
    }).ToArray()
);

```
If you just need a sum of all non zero values in all the dictionary items you can use

```
ret.Sum(x => x.Value.Count(y => y != 0));

```

If you need to iterate through all the key value pairs and you wish not to use a foreach loop then you have to come up with an extension method of your own as shown below

```
 public static class DictionaryExtensionMethods
{
    public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> method)
    {
        foreach (T obj in enumerable)
        {
            method(obj);
        }
    }
}

```

And you can use it like this

```
 class Program
{
    static void Main()
    {
       var ret = new Dictionary<string, int[]>();
        int[] a = { 1, 0, 3, 4, 0 };
        int[] b = { 3, 0, 9, 10, 0 };
        int[] c = { 2, 3, 3, 5, 0 };
        ret.Add("Jack", a);
        ret.Add("Jane", b);
        ret.Add("James", c);
        ret.ForEach(x => Write(x.Value.Count(y => y !=0), x.Key));
        Console.ReadLine();
    }

    public static void Write(int count, string key)
    {
        Console.WriteLine("Count of non zeroes in {0} is {1}", key, count);
    }
}

```