从c#中的IGrouping获取对象

本文关键字:获取 取对象 IGrouping 中的 | 更新日期: 2023-09-27 18:19:38

我需要从query2访问可用的酒店对象,在这里我可以使用y.key访问HotelCode值,但我如何从query2中访问可用的Hotel对象。

我的矩阵MOdel

 public  class JsonMatrixModel
        {
            public class Result
            {
                public string responseId { get; set; }
                public string searchId { get; set; }
                public int totalFound { get; set; }
                public List<availableHotels> availableHotels { get; set; }
            }
            public class availableHotels
            {
                public string processId { get; set; }
                public string hotelCode { get; set; }
                public string availabilityStatus { get; set; }
                public double totalPrice { get; set; }
                public double totalTax { get; set; }
                public double totalSalePrice { get; set; }
                public string currency { get; set; }
                public string boardType { get; set; }
                public List<rooms> rooms { get; set; }

            }
            public class rooms
            {
                public string roomCategory { get; set; }
                public List<paxes> paxes { get; set; }
                public double totalRoomRate { get; set; }
                public List<ratesPerNight> ratesPerNight { get; set; }
            }
            public class paxes
            {
                public string paxType { get; set; }
                public int age { get; set; }

            }
            public class ratesPerNight
            {
                public string date { get; set; }
                public double amount { get; set; }
            }
        }

我的查询

Enumerable<IGrouping<string, JsonMatrixModel.availableHotels>> quer2 =
      from ff in ddd
      from ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice) group ss by ss.hotelCode;

访问价值

  foreach (var y in quer2)
{  
  string ss = y.Key;
}

从c#中的IGrouping获取对象

进行分组后,投影到一个新的匿名对象,该对象的属性将具有键的值,另一个可以是该键的分组值列表。

var quer2 =
      from  ff in ddd
      from  ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice) 
      group ss by ss.hotelCode
      select new
      {
        GroupKey = ss.Key,      
        GroupValuesList =  ss.ToList()
      };

Console.WriteLine(quer2.First().GroupKey);

IGrouping只是一个带有附加Key属性的IEnumerable。这是你想要的吗?

var groups = items.GroupBy(p => p.Property);
foreach (var group in groups)
{
    Console.WriteLine(group.Key);
    foreach (var item in group)
    {
        Console.WriteLine("'t{0}", item.AnotherProperty); 
    }
}