【问题标题】:What is happening to my Group Key after JsonConvert?JsonConvert 之后我的组密钥发生了什么变化?
【发布时间】:2018-02-22 23:38:15
【问题描述】:

我正在尝试做一个 linq 组,它似乎正在工作,但是当我执行 JsonConvert 时,它似乎很多。

public class StatsVm
{

        public int TotalCount { get; set; }
        public string Name { get; set; }
        public DateTime? LoggedDate { get; set; }
}

假设我有一个包含 10 个对象的数组,其中 5 个的 LoggedDate 为 2018-02-19,其中 5 个的 LoggedDate 为 2018-02-20

我想按日期(不是时间部分)对它们进行分组

var d = results.GroupBy(group => group.LoggedDate.GetValueOrDefault().ToString("yyyy-MM-dd")).ToList();

这给了我一个List<IGrouping<string, StatsVm>>,这是我想要的,但是当我这样做时

JsonConvert.SerializeObject(d)

我丢失了分组密钥。我想它可能不是有效的 json 或其他东西。但我想在 json 中有这样的东西。

{
    "2018-1-1": [
        {
            "LoggedDate": "2018-1-1",
            "Name": "Test",
            "TotalCount": 20
        },
        {
            "LoggedDate": "2018-1-1",
            "Name": "Test2",
            "TotalCount": 1
        }
    ],
    "2018-1-2": [
        {
            "LoggedDate": "2018-1-2",
            "Name": "Test",
            "TotalCount": 20
        },
        {
            "LoggedDate": "2018-1-2",
            "Name": "Test2",
            "TotalCount": 1
        }
    ]
}

【问题讨论】:

    标签: c# json linq


    【解决方案1】:

    通过使用字典,我能够创建您需要的结果。

    示例代码:

    void Main()
    {
        // Create dummy data
        var results = new List<StatsVm>();
        // Create dummy data
        var results = new List<StatsVm>();
        results.Add(new StatsVm { TotalCount = 20, Name = "Test", LoggedDate = new DateTime(2018, 1, 1) });
        results.Add(new StatsVm { TotalCount = 1, Name = "Test2", LoggedDate = new DateTime(2018, 1, 1) });
        results.Add(new StatsVm { TotalCount = 20, Name = "Test", LoggedDate = new DateTime(2018, 1, 2) });
        results.Add(new StatsVm { TotalCount = 1, Name = "Test2", LoggedDate = new DateTime(2018, 1, 2) });
    
        // Creates the dictionary
        var output = results
            .Select(r => new
            {
                LoggedDate = r.LoggedDate.GetValueOrDefault().ToString("yyyy-MM-dd"),
                Name = r.Name,
                TotalCount = r.TotalCount
            })
            .GroupBy(group => group.LoggedDate)
            .ToDictionary(t => t.Key);
    
        // Serializes the dictionary as a JSON string
        var serializedString = JsonConvert.SerializeObject(output, Newtonsoft.Json.Formatting.Indented);
    
        // Prints the serialized string
        Console.WriteLine(serializedString);
    }
    
    public class StatsVm
    {
    
        public int TotalCount { get; set; }
        public string Name { get; set; }
        public DateTime? LoggedDate { get; set; }
    }
    

    编辑:

    更新代码以将日期格式化为 yyyy-mm-dd

    生成的输出:

    {
      "2018-01-01": [
        {
          "LoggedDate": "2018-01-01",
          "Name": "Test",
          "TotalCount": 20
        },
        {
          "LoggedDate": "2018-01-01",
          "Name": "Test2",
          "TotalCount": 1
        }
      ],
      "2018-01-02": [
        {
          "LoggedDate": "2018-01-02",
          "Name": "Test",
          "TotalCount": 20
        },
        {
          "LoggedDate": "2018-01-02",
          "Name": "Test2",
          "TotalCount": 1
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-27
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多