1

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.

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. I updated the question. My actual class objects are quite lengthy so was just trying to keep things concise for the question.
This does not answer the question, this specifically asks for an AutoMapper solution. I don't necessarily disagree with the answer but I came here from the search looking for an AM solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.