Solved

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

Posted on 2014-01-26
2
5,638 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I'd like to talk about something that is near and dear to my heart: build systems. Without them, building software is all about compiling locally, with software versions everywhere. It can be a mess. Today we are going to discuss building a small di…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

785 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