如何在C#中解压缩数组?

时间:2016-04-14 04:41:37

标签: c# arrays

我没有关于如何解压缩数组的想法(请求数组A []到响应数组B [])

以下是我的定义

A 是请求类。

class A
{
   public string Date { get; set; }
   public decimal Price { get; set; }
}

以下是我对A类及其初始化的请求数组。

var request = new A[]
  {
      new A { Date = "14-04-2016", Price = 100 },
      new A { Date = "15-04-2016", Price = 100 },
      new A { Date = "16-04-2016", Price = 0 },
      new A { Date = "17-04-2016", Price = 100 },
      new A { Date = "18-04-2016", Price = 100 }
  };

B 是回复课程。

class B
{
   public string Start { get; set; }
   public string End { get; set; }
   public decimal Price { get; set; }
}

上述请求需要转换为B数组。像这样的东西

var response = new B[]
  {
      new B { Start = "14-04-2016", End = "16-04-2016", Price = 100 },
      new B { Start = "16-04-2016", End = "17-04-2016", Price = 0 },
      new B { Start = "17-04-2016", End = "18-04-2016", Price = 100 }
  }; 

响应按价格和按日期排序进行分组。更像是,我需要将请求数组A []解压缩到响应数组B []。

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

您可以使用GroupBy linq扩展,在查询返回List<B>个对象后执行此操作。

var results = request.Select(s=> 
           new 
           {
               Price = s.Price, 
               Date = DateTime.ParseExact(s.Date, "dd-MM-yyyy", null) // convert to Date.
           })
   .GroupBy(g=>g.Price)
   .Select(s=>
           new B() 
           {
               Start = s.Min(c=>c.Date).ToString("dd-MM-yyyy"),
               End = s.Max(c=>c.Date).ToString("dd-MM-yyyy"),
               Price = s.Key   
           })
   .ToList() ;

更新:

根据评论,您实际上并不需要对价格进行分组。您需要的是对价格匹配的相邻项目进行分组。

我们可以通过稍微修改以上Linq查询来实现此目的。

    int grp = 0;
    decimal prevprice=response.First().Price;
    var results = request.Select((s, i)=> 
                  {

                      grp = s.Price == prevprice? grp : ++grp;                       
                      prevprice = s.Price;
                       return new 
                       {
                           grp,
                           Price = s.Price, 
                           Date = DateTime.ParseExact(s.Date, "dd-MM-yyyy", null)
                       };
                  })
           .GroupBy(g=>g.grp)
           .Select(s=>
                   new B() 
                   {
                       Start = s.Min(c=>c.Date).ToString("dd-MM-yyyy"),
                       End = s.Max(c=>c.Date).ToString("dd-MM-yyyy"),
                       Price = s.First().Price   
                   });

<强>输出

14-04-2016,15-04-2016 ,100 
16-04-2016,16-04-2016 ,0 
17-04-2016,18-04-2016 ,100

工作Example

答案 1 :(得分:1)

伪代码(假设请求按日期排序 - 如果不是,您可以轻松排序):

int lastPrice = -1;

//count the distinct price ranges
int responseSize = 0;
foreach (A requestObj in request) {
 if (requestObj.price != lastPrice) {
  responseSize++;
  lastPrice = requestObj.price;
 }
}


//set the initial element
B[] response = new B[responseSize];
response[0].start = request[0].date;
response[0].price = request[0].price;
int responseindex = 0;

//parse the result
foreach (A requestObj in request) {
 if (requestObj.price != response[responseindex].price) {
  response[responseindex].end = requestObj.date;
  responseIndex++;
  response[responseindex].price = requestObj.price;
  response[responseindex].start= requestObj.date;
 }
}

//set the end date of the final object
response[response.length - 1].end = request[request.length - 1].date;

答案 2 :(得分:0)

这也可以通过以下方式完成:

var response = from reqItem in request
group reqItem by reqItem.Price into g
select new B()
{ 
    Start = g.Min(m => DateTime.Parse(m.Date)).ToString("dd-MM-yyyy"),
    End = g.Max(m => DateTime.Parse(m.Date)).ToString("dd-MM-yyyy"),
    Price = g.Key
};