How to get the max value for each date group

T

Tony

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
 
B

bradbury9

El martes, 14 de mayo de 2013 09:39:47 UTC+2, Tony escribió:
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

This link is not dataset specific, but should help. It is about grouping inLinq. http://smehrozalam.wordpress.com/tag/ranking-functions/
 
A

Arne Vajhøj

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top