繁体   English   中英

在C#中从IGrouping获取对象

[英]Getting the Object From IGrouping in c#

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

我的矩阵模型

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

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

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

IGroupping只是具有附加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); 
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM