I have the program below:
void Main()
{
List<Online_video_for_programmers> current_year_videos = new List<Online_video_for_programmers>()
{
new Online_video_for_programmers
{
video_name="websocket for fortran programmers",
season=2,
seasontype=1,
year=2012
},
new Online_video_for_programmers
{
video_name="cross site scripting for facebook addicts",
season=3,
seasontype=2,
year=2012
},
new Online_video_for_programmers
{
video_name="Artificial intelligence for self aware robots",
season=1,
seasontype=1,
year=2012
}
,
new Online_video_for_programmers
{
video_name="Artificial intelligence for self aware robots",
season=3,
seasontype=1,
year=2012
},
new Online_video_for_programmers
{
video_name="Artificial intelligence for adavanced robots",
season=5,
seasontype=1,
year=2012
},
new Online_video_for_programmers
{
video_name="Yoga for programmers",
season=2,
seasontype=2,
year=2012
},
new Online_video_for_programmers
{
video_name="Yoga for programmers",
season=3,
seasontype=2,
year=2012
}
};
//Last years videos sold
List<Online_video_for_programmers> last_year_videos = new List<Online_video_for_programmers>()
{
new Online_video_for_programmers
{
video_name="websocket for fortran programmers",
season=2,
seasontype=1,
year=2011,
},
new Online_video_for_programmers
{
video_name="cross site scripting for facebook addicts",
season=3,
seasontype=1,
year=2011,
},
new Online_video_for_programmers
{
video_name="Artificial intelligence for self aware robots",
season=2011,
seasontype=3,
year=2011,
}
,
new Online_video_for_programmers
{
video_name="Yoga for programmers",
season=4,
seasontype=2,
year=2011,
},
new Online_video_for_programmers
{
video_name="Yoga for programmers",
season=2,
seasontype=2,
year=2011
}
};
}
// Define other methods and classes here
public class Online_video_for_programmers
{
public int seasontype {get;set;} //this can be the id of a year, a quater or month Tyepes are 1: months, 2, for quarters, 3 for year
public int season {get;set;} // when season type is 1, season =1 means january.
//When season type is 2, season=3 means 3rd quater. When seasontype is 3, season =2011 or 2010 etc...
public string video_name {get;set;} //The nambe of the video
public int year {get;set;} // The year the video was published
}
I would like to use current_year_videos and last_year_videos to retreive what titles where published last year and not this year. For this i use the following code:
var result=current_year_videos.Concat(last_year_videos).GroupBy( g=> new { seasontype,season,video_name }).Select(s=> new Online_video_for_programmers{ seasontype=s.Key.seasontype, season=s.Key.season, video_name=s.Key.video_name });
The code above gives me the ones that were produced both this year and last year, so that i can easily find the missing ones.
And here is the problem. The video “Artificial intelligence for self aware robot” was produced for the entire year in 2011 while there were 3 monthly productions in 2012.
I do not want to see this as missing for 2012. It should only be missing if it was not produced at all in 2012. The question is, how to adjust my query to include this condition in the result.
I spent some time figuring different things with no success so far.
Hope someone can help.
Many thanks in advance
2
Answers
Gives:
UPDATE: If you want to deal with season type (month, quarter, year) when checking videos, then you can create method which converts season and season type to list of months:
With this method you can check if video has months intersections with same video from current year:
Returns:
You want to find:
but for that, there is no need for grouping:
All you have to do is to check if the video with the selected key (
video_name
) exists in the last year’s collection.