用linq将数据格式化为一个新的对象。

问题描述 投票:0回答:1

我把下面这个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 
    };
});
c# linq
1个回答
1
投票

你可以使用 Select 的方法来推算每一个 Weight 例子 Info. 然后将结果分配给一个单独的变量(假设 data 的实例 Data 阶级)

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

Foreach 在你的代码中,方法不能返回任何东西


1
投票

你是在正确的轨道上 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 })

这里: data 是一个 IEnumerable<Data>.


0
投票

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

Data 反对 Info 对象的参数的LINQ查询中发生。infoList.AddRange(...);.

GetData() 是一个本地函数,它只是建立了一个 IList<Data> 含有几个 Data 对象,每个对象都有一个嵌入的 IList<Weight> 集。其结果应类似于""的内容。des 对象。

请注意 .SelectMany() 查询 IList<Data> 所回 GetData() 并返回一个 IEnumerable<> 包含了它所调用的集合中每个对象的一个或多个对象。请注意 .Select().SelectMany() 不同的是 .Select() 只为原始集合中的每个对象产生一个输出对象,而 .SelectMany() 可以为每个输入对象返回多个输出对象。

的参数,可以返回多个输出对象。.SelectMany() 是一个lambda。d => d.Weight.Select(w => ...). 这一点很重要,因为 Data 类包含一个 集合 (即。IList<Weight>)的Weight对象。这使得我们可以访问 Data 中的对象 d 参数和每个Weight对象--一次一个--在 w 争论。

最后是: .Select(...) 返回一个新的 Info 对象的每个 Weight 如代码所示,包含属性值的对象。

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"
                        }
                    }
                }
            };
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.