将不同对象列表从数据库转换为Entity Framework Core中的对象列表

时间:2019-06-27 19:00:01

标签: c# entity-framework linq entity-framework-core

LINQ的新手,我不确定是否已将问题放入正确的描述中。

要转换垂直结构

[Label]    [Value]
-Field1      1
-Field2      2
-Field3      3

要像下面这样水平对齐

Field1  Field2  Field3 
  1       2       3     

示例:

我从微服务以JSON格式获取的以下对象列表

{
  0: {Name: "Jason" , Age: 20 , Address: "London"}
  1: {Name: "Andy" , Age: 35, Address: "Boston"}
  2: {Name: "Mike", Age : 27,Address: "California"}
  3: {Name: "Maria", Age: 22,Address: "Arizona" }
}

应转换为以下格式

{       
  0: {"Jason","Andy","Mike","Maria"}
  1: {20,35,27,22}
  2: {"London","Boston","California","Arizona"}
}

我在UI前端处理过它,但是我想在服务端对其进行格式化,因此在Json序列化之后,第0、1、2、3索引分别应具有Name,Age和Address。

下面是Model,它将转换后的数据保存在后端。 (它将是模型数据的列表)

public class Reviewer
{
 public string Name { get; set; }

 public int Age { get; set; }

 public string Address {get;set;}
}

我尝试了多种方法来展平数组并将结果转换为上述格式,但仍然找不到正确的方法。

2 个答案:

答案 0 :(得分:0)

您的示例没有道理... 如果您要这样做:

{       
  Name: ["Jason","Andy","Mike","Maria"]
  Age: [20,35,27,22]
  Address: ["London","Boston","California","Arizona"]
}

与3个数组一样,一个带有名称,一个带有年龄,一个带有地址,您可以执行以下操作:

var result = new Reviewer(){
    Name = initialArray.Select(a => a.Name).ToArray(),
    Age = initialArray.Select(a => a.Age).ToArray(),
    Address = initialArray.Select(a => a.Address).ToArray()
};

使用

var initialArray = JsonConvert.Deserialize(@"
[
  {Name: \"Jason\" , Age: 20 , Address: \"London\"},
  {Name: \"Andy\" , Age: 35, Address: \"Boston\"},
  {Name: \"Mike\", Age : 27,Address: \"California\"},
  {Name: \"Maria\" Age: 22,Address: \"Arizona\" }
]
");

如果这不是您的意思,则需要澄清您的问题...您的示例不是有效的JSON,并且您显示的模型类与第一个示例中的数据匹配,而不是您想要的内容。

答案 1 :(得分:0)

关于您的课程,我将int Age更改为字符串Age。因为20、35、27、22不是整数。

public class Reviewer
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Address { get; set; }
}

public static class InitializeAutoMapper
{
    public static void Initialize()
    {
        AutoMapper.Mapper.Initialize(config =>
        {
            config.CreateMap<JArray, Reviewer>()
            .ForMember(dest => dest.Name, opt => opt.MapFrom(so =>
            string.Join(", ", so.Select(x => new JObject(new JProperty("Name", x["Name"]))).Values("Name").ToList())))

            .ForMember(dest => dest.Age, opt => opt.MapFrom(so =>
            string.Join(", ", so.Select(x => new JObject(new JProperty("Age", x["Age"]))).Values("Age").ToList())))

            .ForMember(dest => dest.Address, opt => opt.MapFrom(so =>
            string.Join(", ", so.Select(x => new JObject(new JProperty("Address", x["Address"]))).Values("Address").ToList())));
        });
    }
}

class Program
{
    static void Main(string[] args)
    {
        InitializeAutoMapper.Initialize();

        string json = @"[
                        { Name: ""Jason"" , Age: 20 , Address: ""London""},
                        { Name: ""Andy"" , Age: 35, Address: ""Boston""},
                        { Name: ""Mike"", Age: 27,Address: ""California""},
                        { Name: ""Maria"", Age: 22,Address: ""Arizona"" }]";

        Reviewer reviewer = AutoMapper.Mapper.Map<Reviewer>(JArray.Parse(json));
    }
}

enter image description here