Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-26
2
Medium Priority
?
5,905 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
How many times a day do you open, acknowledge, or close an IT incident? What’s your process? Do you have a process depending on the incident, systems involved, and other factors? New Relic Alerts gives you options for how you interact with notifica…
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 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…

610 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