【问题标题】:Formatting data with linq into a new object使用 linq 将数据格式化为新对象
【发布时间】:2020-04-24 17:57:57
【问题描述】:

我已经反序列化了下面的这个 json 对象,我想在 c# 中使用 linq 将其重新格式化为新对象,我想知道是否有人可以帮助我。

我将对象反序列化为一个类

public class Data
{
    public string title{get;set;};
    public List<Weight> Weight{get;set;}
}

public class Weight
{
    public DateTime Date{get;set;}
    public string weight{get;set;}
}
"title": "Paul weight log",
"weight": [
    {
        "date": "2017-04-21T00:00:00Z",
        "weight": "120kg", 
    },
    {
        "date": "2017-09-15T00:00:00Z",
        "weight": "125kg", 
    },
    {
        "date": "2017-10-27T00:00:00Z",
        "weight": "130kg", 
    },
]
}

这是我现在想要的格式。

"title": "Paul weight log" "date": "2017-04-21T00:00:00Z" "weight": "120kg"
"title": "Paul weight log" "date": "2017-04-21T00:00:00Z" "weight": "125kg"
"title": "Paul weight log" "date": "2017-10-27T00:00:00Z" "weight": "130kg"

我尝试做的是创建一个新类

public class Info
{
    public string title{get;set;}
    public string weight{get;set;}
    public DateTime Date{get;set;}
}

然后我尝试在反序列化对象上使用 foreach。

des.Data.forEach(c =>
{
    new Info()
    {
        Title = c.Title,
        Date = c.date,
        Weight =c.weight 
    };
});

【问题讨论】:

  • 您忘记发布遇到问题的代码。
  • 对不起斯图尔特我认为这将是太多的代码,但进入这个问题,我相当坚持它。我也尝试过使用 selectmany
  • 使用select() linq 扩展

标签: c# json linq


【解决方案1】:

您可以使用Select 方法将每个Weight 实例投影到Info。然后将结果分配给单独的变量(假设dataData 类的实例)

var result = data.Weight
    .Select(w => new Info { Date = w.Date, weight = w.weight, title = data.title })
    .ToList();

您代码中的Foreach 方法无法返回任何内容

【讨论】:

    【解决方案2】:

    你在SelectMany 的正确轨道上,因为你必须创建一个扁平化的Data 列表乘以他们的Weights:

    from d in data
    from w in d.Weight
    select new
    {
        d.title,
        w.Date,
        w.weight
    }
    

    表面下等于SelectMany

    data.SelectMany(d => d.Weight, 
        (d, w) => new { d.title, w.Date, w.weight })
    

    这里,dataIEnumerable&lt;Data&gt;

    【讨论】:

      【解决方案3】:

      使用您的原始类定义,我组合了一个小型、可运行的 ASP.NET Core 控制台应用程序(请参见下面的代码)。

      Data 对象到Info 对象的转换发生在作为infoList.AddRange(...); 参数的LINQ 查询中。

      GetData() 是一个本地函数,它只构建一个IList&lt;Data&gt;,其中包含几个Data 对象,每个对象都带有一个嵌入的IList&lt;Weight&gt; 集合。结果应该类似于代码中des 对象的内容。

      注意.SelectMany() 查询GetData() 返回的IList&lt;Data&gt; 并返回一个IEnumerable&lt;&gt;,其中包含调用它的集合中每个对象的一个​​或多个对象。请注意.Select().SelectMany() 的不同之处在于.Select() 只为原始集合中的每个对象生成一个输出对象,而.SelectMany() 可以为每个输入对象返回多个输出对象。

      .SelectMany() 的参数是一个 lambda,d =&gt; d.Weight.Select(w =&gt; ...)。这很重要,因为Data 类包含一个collection(即IList&lt;Weight&gt;)Weight 对象。这使我们可以访问d 参数中的Data 对象和w 参数中的每个权重对象(一次一个)。

      最后,.Select(...) 为每个 Weight 对象返回一个新的 Info 对象,其中包含代码中显示的属性值。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      
      namespace LinqReformatObject
      {
          class Program
          {
              static void Main(string[] args)
              {
                  var infoList = new List<Info>();
      
                  infoList.AddRange(
                      GetData()
                          .SelectMany(d =>
                              d.Weight
                                  .Select(w =>
                                      new Info
                                      {
                                          title = d.title,
                                          weight = w.weight,
                                          Date = w.Date
                                      }
                                  )
                          )
                  );
      
                  foreach (var i in infoList)
                  {
                      Console.Write($"\"title\": \"{i.title}\", ");
                      Console.Write($"\"date\": \"{i.Date.ToString("O")}\",");
                      Console.Write($"\"weight\": \"{i.weight}\"");
                      Console.WriteLine();
                  }
      
              }
      
          public class Data
          {
              public string title { get; set; }
              public List<Weight> Weight { get; set; }
          }
      
          public class Weight
          {
              public DateTime Date { get; set; }
              public string weight { get; set; }
          }
      
          public class Info
          {
              public string title { get; set; }
              public string weight { get; set; }
              public DateTime Date { get; set; }
          }
      
              static IList<Data> GetData()
              {
                  return new List<Data>()
                  {
                      new Data() {
                          title = "Paul's weight log",
                          Weight = new List<Weight>() {
                              new Weight () {
                                  Date = DateTime.Parse("2017-04-21T00:00:00Z"),
                                  weight = "120kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-09-15T00:00:00Z"),
                                  weight = "125kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-10-27T00:00:00Z"),
                                  weight = "130kg"
                              }
                          }
                      },
                      new Data() {
                          title = "John's weight log",
                          Weight = new List<Weight>() {
                              new Weight () {
                                  Date = DateTime.Parse("2017-06-21T00:00:00Z"),
                                  weight = "101kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-08-15T00:00:00Z"),
                                  weight = "98kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-11-27T00:00:00Z"),
                                  weight = "94kg"
                              }
                          }
                      },
                      new Data() {
                          title = "Ringo's weight log",
                          Weight = new List<Weight>() {
                              new Weight () {
                                  Date = DateTime.Parse("2017-03-21T00:00:00Z"),
                                  weight = "98kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-06-15T00:00:00Z"),
                                  weight = "100kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-09-27T00:00:00Z"),
                                  weight = "102kg"
                              }
                          }
                      },
                      new Data() {
                          title = "George's weight log",
                          Weight = new List<Weight>() {
                              new Weight () {
                                  Date = DateTime.Parse("2017-01-21T00:00:00Z"),
                                  weight = "99kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-03-15T00:00:00Z"),
                                  weight = "103kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-05-17T00:00:00Z"),
                                  weight = "113kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-07-19T00:00:00Z"),
                                  weight = "111kg"
                              },
                              new Weight () {
                                  Date = DateTime.Parse("2017-09-23T00:00:00Z"),
                                  weight = "109kg"
                              }
                          }
                      }
                  };
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 2018-10-26
        • 1970-01-01
        相关资源
        最近更新 更多