Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now