Search code examples
c#asp.netrepeater

Selecting additional non key fields in IEnumerable


I have a class

 public class AmenityShowtime
    {
        public String AmenityKey { get; set; }
        public String AmenityIcon { get; set; }
        public String shTimes { get; set; }
    }

Ultimately, I want to have a structure that is comprised of these nested classes:

 public class AmenityShowtime
        {
            public String AmenityKey { get; set; }
            public String AmenityIcon { get; set; }
            public String shTimes { get; set; }
        }
    // Movie Class 
    public class theMovie
    {
        public String Movie_title { get; set; }
        public String Rating { get; set; }
        public String RunTime { get; set; }
        public List<theAmenities> amens { get; set; }
    }

    public class theAmenities
    {
        public String AmenityName { get; set; }
        public String AmenityIcon { get; set; }
        public List<theTimes> times { get; set; }
    }

    public class theTimes
    {
        public String timepref { get; set; }
    }

I needed to group by AmenityKey and shtimes ... I used the following code:

            IEnumerable<IGrouping<string, string>> query = amShow.GroupBy(ams => ams.AmenityKey, ams => ams.shTimes);


            List<theAmenities> thisMoviesList = new List<theAmenities>();

            foreach (IGrouping<string, string> grp in query)
            {
                theAmenities thisMovieAmenities = new theAmenities();
                thisMovieAmenities.AmenityName = grp.Key;


                List<theTimes> thisMovieTimes = new List<theTimes>();

                foreach (string stimes in grp)
                {
                    theTimes thisShowtime = new theTimes();
                    thisShowtime.timepref = stimes;
                    thisMovieTimes.Add(thisShowtime);
                }

                thisMovieAmenities.times = thisMovieTimes;
                thisMoviesList.Add(thisMovieAmenities);
            }

works great, with one exception ... how do I get access to the field: AmenityIcon in the

foreach (IGrouping<string, string> grp in query)
                {
                    theAmenities thisMovieAmenities = new theAmenities();
                    thisMovieAmenities.AmenityName = grp.Key;

I want to be able to do the following:

thisMovieAmenites.AmenityIcon = AmenityIcon

I must be missing something, thank you in advance


Solution

  • If you want to include the amenityIcon in the grouping, then one way is this:

    var query = amShow.GroupBy(ams => new {ams.AmenityKey, ams.AmenityIcon}, 
                               ams => ams.shTimes);
    

    Note that you do need the var keyword now, since this is an IGrouping<Anonymous-type,String>

    (This will group based on key and icon, I am assuming that the icon is the same if the key is the same, so this is essentially the same as grouping only by key)

    Now you can do

    foreach (var grp in query)
    {
         theAmenities thisMovieAmenities = new theAmenities();
         thisMovieAmenities.AmenityName = grp.Key.AmenityKey;
         thisMovieAmenities.AmenityIcon = grp.Key.AmenityIcon;
         ...
    

    If you want to avoid anonymous types, you could also create your class theAmenities right in the grouping:

    IEnumerable<IGrouping<theAmenities,string>> query = amShow.GroupBy(
         ams => new theAmenities(){AmenityKey = ams.AmenityKey, AmenityIcon = ams.AmenityIcon}, 
         ams => ams.shTimes);
    

    but that requires that your class theAmenities implements the IEquatable<T> interface, to allow the GroupBy operator to recognize all theAmenities objects with the same key as equal. If you use an anonymous type instead, this will work automatically. But this approach has the advantage that you could let your IEquatable ignore the AmenityIcon, if indeed it is possible that there are multiple items with the same key but different icons