0

I am currently fetching a list of object from the database and would like to return it in a certain way from my ASP.Net Web API with Automapper. My Object currently looks like this after fetching it from the database:

[
    {
        "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"
    }
]

But I would like it to look like this:

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

Currently my CreateMap looks like this

CreateMap<DropdownValue, DropdownValueListDto>();

And my DropdownValueListDto like this

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

My LINQ Operation looks like this:

    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;
    }

It would be great if someone could help me achieve this.

Thanks in advance

5
  • You don't want a simple map here, this is more of a transformation operation. - you could use linq to objects for this Commented Jul 10, 2018 at 16:15
  • I inserted my code for my LINQ operation above. How would I use the LINQ to objects with it? Commented Jul 10, 2018 at 16:21
  • 1
    Your JSON showing how you WANT the data to look doesn't match your DropDownValueListDto object... Is the JSON stuff more like you expect to see? Commented Jul 10, 2018 at 16:28
  • Yes the JSON stuff is more like I expect to see it. I tried to modify the DropdownValueListDto to match how I would like to see it, but that didn't work out. Commented Jul 10, 2018 at 16:30
  • ok, I'll throw an answer together.... give me a mo... Commented Jul 10, 2018 at 16:32

1 Answer 1

1

based on your edits, I guess you'd have to do something like this:

//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)
         });

That should give you something like what you want - I've not tested this though...

Sign up to request clarification or add additional context in comments.

1 Comment

@EE - no problem. I've realised this morning that I didn't do anything with the line declaring result as a new List<DropDownListDto> so I've removed that, and added a comment such that the result variable is actually going to be an IEnumerable<DropDownListDto>. You could cast this using .ToList() if you need an actual List<> type but it'll probably do what you want as-is.

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.