Solved

c# Linq, List, GroupBy or Distinct and max(date)

Posted on 2014-01-26
2
5,678 Views
Last Modified: 2016-02-10
With this example of a List (weatherforecast )- Week Day, Local, Weather, Temp, Forecast Date)

"Tuesday", "Lisbon", "good", "19", "24-Jan-14 "  -----> ok
"Friday", "Lisbon", "cloudy", "20", "24-Jan-14 "  -----> ok
"Sunday", "Troia", "good", "20", "24-Jan-14 "   -----> ok
"Tuesday", "Lisbon", "good", "18", "22-Jan-14"
"Friday",  "Praia Verde, "excellent", "20", "22-Jan-14" -----> ok
"Sunday", "Troia", "good", "20", "22-Jan-14"

I want the latest forecasts (4 rows here) so I need to use  max(date) somewhere.  The max(date) may not be all equal.

For Lisbon and Troia I will have only 24 Jan, but for "Praia Verde" I will have the 22 Jan.

I want to put in a grid all results that are wit ----> ok

Struggling with this! Tks for any help
0
Comment
Question by:novreisb
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39811612
Hi,

Check my example:

void Main()
{
        // Initialization of the list
	List<WeatherForeCast> WeatherForeCastList = new List<WeatherForeCast>()
	{
		new WeatherForeCast(){WeekDay = "Tuesday",Local = "Lisbon", Weather = "good", Temp = 19, ForeCastDate = DateTime.Parse("24-01-2014")},
		new WeatherForeCast(){WeekDay = "Friday",Local = "Lisbon", Weather = "cloudy", Temp = 20, ForeCastDate = DateTime.Parse("24-01-2014")},
		new WeatherForeCast(){WeekDay = "Sunday",Local = "Troia", Weather = "good", Temp = 20, ForeCastDate = DateTime.Parse("24-01-2014")},
		new WeatherForeCast(){WeekDay = "Tuesday",Local = "Lisbon", Weather = "good", Temp = 18, ForeCastDate = DateTime.Parse("22-01-2014")},
		new WeatherForeCast(){WeekDay = "Friday",Local = "Praia Verde", Weather = "excellent", Temp = 20, ForeCastDate = DateTime.Parse("20-01-2014")},
		new WeatherForeCast(){WeekDay = "Sunday",Local = "Troia", Weather = "good", Temp = 20, ForeCastDate = DateTime.Parse("22-01-2014")},
	};
	

        //This will get the required results
	WeatherForeCastList
        //First group by region (Local)
	.GroupBy
	(
		x=>x.Local
	)
        //From the group, only select those rows, that have a ForecastDate equal to the max ForecastDate of each group. This will create a list of lists of WeatherForeCast class (check the following screenshot to see how they look in LinqPad.
	.Select
	(
		x=>
		x
		.Where
		(
			z=>
			z.ForeCastDate == x.Max (d => d.ForeCastDate)
		)
		.Select 
		(
			z => new WeatherForeCast
				{
					WeekDay=z.WeekDay,
					Local = z.Local,
					Weather = z.Weather,
					Temp = z.Temp,
					ForeCastDate = z.ForeCastDate
				}
		)
	)
        //The following is necessary in order to get only one flat list of WeatherForeCast objects
	.SelectMany 
	(
		x => x
	);
}

public class WeatherForeCast
{
    public string WeekDay {get;set;}
    public string Local{get;set;}
    public string Weather {get;set;}
    public int Temp{get;set;}
    public DateTime ForeCastDate{get;set;}
}

Open in new window


GroupBySelectSelectMany
0
 

Author Closing Comment

by:novreisb
ID: 39812994
Hi jyparask,

My instinct pointed to a GroupBy  with two variables (Local, Weekday) but the results shows what I want. Thank you very much!

Luis
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question