AutoMapper根据嵌套值重新映射对象

时间:2018-07-10 16:00:02

标签: c# asp.net arrays json automapper

我当前正在从数据库中获取对象列表,并希望以某种方式从我的带有Automapper的ASP.Net Web API中返回它。从数据库中获取“我的对象”后,当前看起来像这样:

[
    {
        "id": 1,
        "dropdown": "Country",
        "value": "Germany"
    },
    {
        "id": 2,
        "dropdown": "Country",
        "value": "United States"
    },
    {
        "id": 5,
        "dropdown": "Type",
        "value": "Lead"
    },
    {
        "id": 6,
        "dropdown": "Type",
        "value": "Account"
    },
    {
        "id": 7,
        "dropdown": "Type",
        "value": "Reseller"
    },
    {
        "id": 8,
        "dropdown": "Type",
        "value": "Distributor"
    }
]

但是我希望它看起来像这样:

[{
    "Countries": [{
        "id": 1,
        "value": "Germany"
      },
      {
        "id": 2,
        "value": "United States"
      }
    ]
  },
  {
    "Type": [{
        "id": 5,
        "value": "Lead"
      },
      {
        "id": 6,
        "value": "Account"
      }
    ]
  }
]

当前,我的CreateMap看起来像这样

CreateMap<DropdownValue, DropdownValueListDto>();

我的DropdownValueListD就像这样

public class DropdownValueListDto
{
    public int Id { get; set; }
    public string Dropdown { get; set; }
    public string Value { get; set; }
}

我的LINQ操作如下:

    public async Task<IEnumerable<DropdownValue>> GetDropdownValues(string[] dropdowns)
    {
        var dropdownValues = _context.DropdownValues.OrderBy(x => x.Id).ThenBy(x => x.Dropdown).AsQueryable();

        if (dropdowns.Length != 0 || dropdowns != null)
        {
            dropdownValues = dropdownValues.Where(x => dropdowns.Contains(x.Dropdown));
        }

        var dropdownValuesToReturn = await dropdownValues.ToListAsync();

        return dropdownValuesToReturn;
    }

如果有人可以帮助我实现这一目标,那就太好了。

预先感谢

1 个答案:

答案 0 :(得分:1)

根据您的修改,我想您必须执行以下操作:

//Your result class should actually look like this to match JSON:
class DropDownListDto
{
    public string Description {get;set;}
    //here you need an IEnumerable to store the list of Ids and values associated
    //with this drop-down.  you could create a class and declare a LIST<> of that
    //but I'll just use a dictionary instead.
    public Dictionary<int, string> DropDownValues{get;set;}
}

//Get source data
var source = await GetDropDownValues(<insert [] here>);

//transform list into IEnumerable<DropDownListDto>
var result = source
    //if we GROUP BY the Dropdown property then we'll get a distinct list of
    //of drop-downs.
    .GroupBy(x=>x.DropDown)
    //can then get the list of values and Ids from the resulting groups
    .Select(g=>new DropDownListDto
         {
              //key is the grouped by thing - drop down name..
              Description = g.Key,
              //doing a .select from the group result gives the elements
              //in that group only.  Get an anonymous type and cast that
              //to our required dictionary type
              DropDownValues = g
                .Select(x=>new{ key=x.Id, value = x.Value})
                .ToDictionary(k=>k.key, v=>v.value)
         });

这应该会给您一些您想要的东西-尽管我尚未对此进行测试...