skip to Main Content

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


  1. var result = 
        last_year_videos
           .Select(v => new { v.video_name, v.season })
           .Except(current_year_videos.Select(v => new { v.video_name, v.season }));
    

    Gives:

    { video_name = "Artificial intelligence for self aware robots", season = 2011 }
    { video_name = "Yoga for programmers", season = 4 }
    

    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:

    private static IEnumerable<int> GetMonths(Online_video_for_programmers video)
    {
        switch (video.seasontype)
        {
            case 1: return Enumerable.Range(video.season, 1);
            case 2: return Enumerable.Range((video.season - 1) * 3 + 1, 3);
            case 3: return Enumerable.Range(1, 12);
            default:
                throw new ArgumentException("Unknown season type");
        }
    }
    

    With this method you can check if video has months intersections with same video from current year:

    var currentVideos = 
        current_year_videos.Select(c => new { c.video_name, months = GetMonths(c) });
    
    var result = from v in last_year_videos
                 let months = GetMonths(v)
                 where !currentVideos.Any(cv => 
                           cv.video_name == v.video_name &&
                           months.Intersect(cv.months).Any())
                 select v.video_name;
    

    Returns:

    "cross site scripting for facebook addicts" // Month 3
    "Yoga for programmers" // Months 9,10,11
    
    Login or Signup to reply.
  2. You want to find:

    what titles where published last year and not this year

    but for that, there is no need for grouping:

    var result = last_year_videos
        .Where(vLast => !current_year_videos
            .Select(vCurrent => vCurrent.video_name).Contains(vLast.video_name));
    

    All you have to do is to check if the video with the selected key (video_name) exists in the last year’s collection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search