Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the max value for each date group

17 views
Skip to first unread message

Tony

unread,
May 14, 2013, 3:39:47 AM5/14/13
to
I have a typed dataset with several columns but there are only two that are
of interest here. They are myDate and myLoopnrdag
I want to find the maximum value of myLoopnrdag for each group of mydate.

In this example I want to find 11 for myDate 2013-05-03 and
9 for myDate 2013-05-05 and
12 for myDate 2013-05-08 and
8 for myDate 2013-05-10

How do I write a Linq to dataset query that do that ?

Here is an example
myDate myLoopnrdag
2013-05-03 5
2013-05-03 11
2013-05-03 4

2013-05-05 2
2013-05-05 9
2013-05-05 3

2013-05-08 12
2013-05-08 10
2013-05-08 7

2013-05-10 6
2013-05-10 8
2013-05-10 1

//Tony

bradbury9

unread,
May 14, 2013, 3:59:46 AM5/14/13
to
This link is not dataset specific, but should help. It is about grouping in Linq. http://smehrozalam.wordpress.com/tag/ranking-functions/

Arne Vajhøj

unread,
May 18, 2013, 9:58:46 PM5/18/13
to
The problem consist of two parts.

1) get from typed data set to ienumerable<>
2) do the group by on the ienumerable<>

re 1)

You should use the AsEnumerable method.

re 2)

Example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace E
{
public class Data
{
public DateTime DT { get; set; }
public int V { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List<Data> lst = new List<Data> {
new Data { DT=DateTime.Parse("2013-05-03"), V=5 },
new Data { DT=DateTime.Parse("2013-05-03"), V=11 },
new Data { DT=DateTime.Parse("2013-05-03"), V=4 },
new Data { DT=DateTime.Parse("2013-05-05"), V=2 },
new Data { DT=DateTime.Parse("2013-05-05"), V=9 },
new Data { DT=DateTime.Parse("2013-05-05"), V=3 },
new Data { DT=DateTime.Parse("2013-05-08"), V=12 },
new Data { DT=DateTime.Parse("2013-05-08"), V=10 },
new Data { DT=DateTime.Parse("2013-05-08"), V=7 },
new Data { DT=DateTime.Parse("2013-05-10"), V=6 },
new Data { DT=DateTime.Parse("2013-05-10"), V=8 },
new Data { DT=DateTime.Parse("2013-05-10"), V=1 }

};
foreach(Data d in lst.GroupBy(d => d.DT).Select(g => new
Data { DT=g.Key, V=g.Max(d2 => d2.V) }))
{
Console.WriteLine(d.DT + " " + d.V);
}
Console.ReadKey();
}
}
}

Arne


0 new messages