?
Solved

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

Posted on 2014-01-26
2
Medium Priority
?
5,832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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 is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 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