在IEnumerable中选择其他非关键字段

时间:2013-12-19 02:02:03

标签: c# asp.net repeater

我有一个班级

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

最终,我希望有一个由这些嵌套类组成的结构:

 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; }
    }

我需要按AmenityKey和shtimes进行分组......我使用了以下代码:

            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);
            }

效果很好,但有一个例外......如何访问该字段:

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

我希望能够做到以下几点:

thisMovieAmenites.AmenityIcon = AmenityIcon

我一定错过了什么,谢谢你提前

1 个答案:

答案 0 :(得分:0)

如果您想在分组中包含amenityIcon,那么可以采用以下方法:

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

请注意,您现在确实需要var关键字,因为这是IGrouping<Anonymous-type,String>

(这将基于密钥图标进行分组,我假设如果密钥相同,图标是相同的,所以这与仅按密钥分组基本相同)

现在你可以做到

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

如果您想避免匿名类型,您还可以在分组中创建您的类theAmenities:

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

但这需要您的类theAmenities实现IEquatable<T>接口,以允许GroupBy操作符识别具有相同键的所有设备对象。 如果您使用匿名类型,则会自动运行。但是这种方法的优点是你可以让你的IEquatable忽略AmenityIcon,如果确实有多个项目可能有相同的键但是图标不同