从Linq中一周内重复记录中更新最后一个的值

时间:2017-02-04 19:44:38

标签: c# linq

我从JSON文件获取数据,如下所示。如果在同一周的同一个值中有相同的值,我需要将“0”的值赋予“值”。我怎么能在LINQ中做到这一点?例如:

INPUT

ID NAME         INFOID       DATE                       VALUE                           WEEK
2  AAA          31           1.11.2016 00:00:00         1,3                             44
2  AAA          32           2.11.2016 00:00:00         9,1                             44
2  AAA          33           3.11.2016 00:00:00         9,1                             44
2  AAA          34           4.11.2016 00:00:00         0,19                            44
2  AAA          37           7.11.2016 00:00:00         1,2                             45
2  AAA          38           8.11.2016 00:00:00         0,10                            45
2  AAA          39           9.11.2016 00:00:00         5,7                             45
2  AAA          40          10.11.2016 00:00:00         5,7                             45
2  AAA          41          11.11.2016 00:00:00         1,7                             45


OUTPUT

ID NAME         INFOID       DATE                       VALUE                           WEEK
2  AAA          31           1.11.2016 00:00:00         1,3                             44
2  AAA          32           2.11.2016 00:00:00         9,1                             44
2  AAA          33           3.11.2016 00:00:00         0                               44
2  AAA          34           4.11.2016 00:00:00         0,19                            44
2  AAA          37           7.11.2016 00:00:00         1,2                             45
2  AAA          38           8.11.2016 00:00:00         0,10                            45
2  AAA          39           9.11.2016 00:00:00         5,7                             45
2  AAA          40          10.11.2016 00:00:00         0                               45
2  AAA          41          11.11.2016 00:00:00         1,7                             45

预期结果应高于(输出)。

public class DataResolver
{    
   public void GetDataFromJson()
   {
     var res = JsonConvert.DeserializeObject<List<MarketData>>(jsonData);
   }    
}
public class DetailInfo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Info
{
    public DetailInfo Detail { get; set; }
    public int InfoId { get; set; }
    public DateTime InfoDate { get; set; }
    public double Value { get; set; }
}

最好的问候

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题:

 public class DetailInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Info
    {
        public DetailInfo Detail { get; set; }
        public int InfoId { get; set; }
        public DateTime InfoDate { get; set; }
        public double Value { get; set; }

        public int Week { get; set; }
    }

public List<Info> RefineData(List<Info> listOfInfo)
        {
            var orderedAndRefinedData = listOfInfo
                                    .OrderBy(p => p.InfoDate)
                                    .Select(p =>
                                              new Info
                                              {
                                                  Detail    = p.Detail,
                                                  InfoId    = p.InfoId,
                                                  InfoDate  = p.InfoDate,
                                                  Value     = p.Value,
                                                  Week      = WeekOfYear(p.InfoDate)
                                              }).ToList();

            var duplicateDataListInWeek = orderedAndRefinedData
                                            .GroupBy(p => new { p.Week, p.Value })
                                            .SelectMany(grp => grp.Skip(1));

            foreach (var dataItem in duplicateDataListInWeek)
            {
                dataItem.Value = 0;
            }

            return orderedAndRefinedData;
        }

        public static int WeekOfYear(DateTime date)
        {
            var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
            return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)),
                                                                    CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        }