Flatten Nested List using AutoMapper

jared

I'm trying to flatten a nested object into a DTO object in .NET 3.5. Most of what I've seen so far is to use AutoMapper to do this (using v1.x since I need to use .NET 3.5, unfortunately):

Here's what a snippet of my class structures look like:

public class RootObject
{
    [JsonProperty("BaseSupplier")]
    public BaseSupplier BaseSupplier { get; set; }

    [JsonProperty("BaseOrderShipmentLineitem")]
    public IList<BaseOrderShipmentLineitem> BaseOrderShipmentLineitem { get; set; }
}

public class BaseSupplier
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

public class BaseOrderShipmentLineitem
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("qty_delivered")]
    public int QtyDelivered { get; set; }

    [JsonProperty("BaseOrderLineitem")]
    public BaseOrderLineitem BaseOrderLineitem { get; set; }    
}

public class BaseOrderLineitem
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("product_sku")]
    public string ProductSku { get; set; }
}

public class ShipmentDetailsDTO
{
    public int BaseOrderShipmentLineitemId { get; set; }
    public string BaseSupplierName { get; set; }
    public string Sku { get; set; }
}

I've been trying something like this:

Mapper.CreateMap<BaseOrderLineitem, ShipmentDetailsDTO>()
    .ForMember(d => d.Sku, opts => opts.MapFrom(s => s.ProductSku));
Mapper.CreateMap<BaseOrderShipmentLineitem, ShipmentDetailsDTO>();
Mapper.CreateMap<RootObject, ShipmentDetailsDTO>()
    .ForMember(d => d.Sku, opts => opts.MapFrom(s => Mapper.Map<IEnumerable<BaseOrderLineitem>, IEnumerable<ShipmentDetailsDTO>>(s.BaseOrderShipmentLineitem.SelectMany(q => q.BaseOrderLineitem)).FirstOrDefault().Sku))
    ;

var model = Mapper.Map<IEnumerable<RootObject>, IEnumerable<ShipmentDetailsDTO>>(obj);

With that above code I'm getting an error on this bit s.BaseOrderShipmentLineitem.SelectMany(q => q.BaseOrderLineitem):

Cannot implicitly convert type 'IEnumerable<?>' to 'IEnumerable<BaseOrderLineitem>'. An explicit conversion exists (are you missing a cast?)

I'm not sure if it's something simple I'm just overlooking or not.

Alex

A far easier way is to write a simple extension method using some LINQ to do this projection yourself. It's easier and more transparent:

public static class MyConversionExtensions
{
    public static IEnumerable<ShipmentDetailsDTO> ToShipmentDetails(this RootObject root)
    {
        return root.BaseOrderShipmentLineitem.Select(x => new ShipmentDetailsDTO() {
            BaseOrderShipmentLineitemId = x.BaseOrderLineitem.Id,
            BaseSupplierName = root.BaseSupplier.Name,
            Sku = x.BaseOrderLineitem.ProductSku
        });
    }
}

Usage:

var shipmentDetails = myRootObject.ToShipmentDetails();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to flatten irregular nested sublists inside a list

Flatten a list with complex nested structure

AutoMapper flatten nested collections

How do you flatten a nested List using round robin in Scala?

flatten nested list by averaging vectors

How to flatten out nested list into one list more efficiently instead of using unlist method?

how to map nested List to another List with AutoMapper

Recursively flatten nested list in python

Flatten nested lists in a list

Nested list comprehension to flatten nested list

How can I flatten a nested class when using an instance of (not static) AutoMapper?

Flatten randomly nested list into a non nested list using Haskell

How to use Automapper to flatten list of entity hierarchies?

Flatten nested objects using Lodash

flatten a nested list by using generator

Flatten list of nested namedtuples to list of dictionaries

Flatten nested list into 1-deep list

How to flatten a nested list in python?

Flatten LINQ/AutoMapper ProjectTo nested children

How to map one object with nested list to list of objects using Automapper or LINQ?

Flatten nested dictionary using glom

How to use AutoMapper for updating Object with nested List using EntityFramework?

Using automapper for nested objects

alternately flatten values of nested list

Flatten deeply nested list of dataframes

Flatten a nested list structure using concatMap gives error

Flatten a partially nested list in Ansible

AutoMapper DTOModel with Nested List to Model

Unflattening data list into a nested list using AutoMapper