简体   繁体   中英

Flattening complex data into a single object

I am trying to abstract data from a complex and create a EventDto. And I was able to do it using foreach but the syntax is dreadful. Is there a better way of writing this code?

 public class EventDtO
 {
    public string Id { get; set; }
    public string Title { get; set; }
    public string CategoryTitle { get; set; }
    public DateTime DateTime { get; set; }
  }

This is the complex object that i am trying to get the data from

public class RootObject
{ 
    public List<Event> Events { get; set; }
}

public class Event
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public List<Category> Categories { get; set; }
    public List<Geometry> Geometries { get; set; }
}

public class Geometry
{
    public DateTime Date { get; set; }
    public string Type { get; set; }
    public List<object> Coordinates { get; set; }
}

    public class Category 
{ 
    public int Id { get; set; }
    public string Title { get; set; }
}

The mapping relationship i want is

EventDto.Id->Event.Id
EventDto.Title->Event.Title
Event.CategoryTitle->Category.Title
Event.DateTime->Geometry.Date

The Category class will only contain one value, but the geometry.Date can have multiple values.

So the output i want is:

Title Categories Date
"Iceberg B42" Sea and Lake Ice 2020-04-23T14:24:00Z
"Iceberg B42" Sea and Lake Ice 2017-09-15T00:00:00Z

I am able to get the correct information if i do the following code.

var Event = new List<EventDTO>();
foreach (var con in content.Events)
{
    var data = new EventDTO
    {
        Title = con.Title,
        Id = con.Id
    };

    foreach (var cat in con.Categories)
    {
        data.CategoriesTitle = cat.Title;
    }

    foreach (var geo in con.Geometries)
    {
        data.DateTime = geo.Date;
        Event.Add(data);
    }
}

An example of the json

   {
        "id": "EONET_2881",
        "title": "Iceberg B42",
             "description": "",
        "categories": [
            {
                "id": 15,
                "title": "Sea and Lake Ice"
            }
        ]
        "geometries": [
            {
                "date": "2017-04-21T00:00:00Z",
                "type": "Point", 
                "coordinates": [ -107.19, -74.63 ]
            },
            {
                "date": "2017-09-15T00:00:00Z",
                "type": "Point", 
                "coordinates": [ -107.11, -74.08 ]
            }
        ]
    }

You weren't creating a new EventDTO for each Geometry. Wouldn't this lead to multiple records with the date of the last one? Is this what you are looking for;

var Event = content.Events.SelectMany(con => 
    con.Geometries.Select(geo => 
        new EventDTO
        {
            Title = con.Title,
            Id = con.Id,
            CategoriesTitle = con.Categories.FirstOrDefault().Title,
            DateTime = geo.Date
        })
    ).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM