2

I have an object that I am converting to JSON to be consumed by Ember.js. Currently I have some child objects that are fully expanded but ember expects just an array of ids on the client side. How can I flatten the object out to be a int[]

items = Mapper.Map<IList<Item>>(client.GetItems());
foreach (var item in items)
{
  int[] choices = item.Choices.Select(x => x.Id).ToArray();

  item.Choices = choices;
}

get an error about not being able to convert to type int[] from IList<Item> how can I cast the property?

example current JSON I produce after serializing

{ "items": [
   {
     "id": 0,
     "name": "Item0",
     "description": "...",
     "choices": [
       { "id": 0, "property": "somevalue" },
       { "id": 1, "property": "somevalue" },
     ]
   },
   {
     "id": 1,
     "name": "Item1",
     "description": "...",
     "choices": [
       { "id": 0, "property": "somevalue" },
       { "id": 1, "property": "somevalue" },
     ]
   }
]}

The JSON I would like to produce

{ "items": [
   {
     "id": 0,
     "name": "Item0",
     "description": "...",
     "choices": [0, 1]
   },
   {
     "id": 1,
     "name": "Item1",
     "description": "...",
     "choices": [0, 1]
   }
]}
5
  • Issue is here: item.Choices = choices; item.Choices is of type List and you are trying to assign choices of type int[]. Commented Dec 16, 2013 at 21:32
  • Yea how can I get around that without creating a completely new class with the only difference that Items is an int[] instead of an IList<>. I want to change the type of that property for this instance of the object. Commented Dec 16, 2013 at 21:34
  • Why don't use SelectMany? Commented Dec 16, 2013 at 21:35
  • You don't need to create another class and you cannot change the property type. You can create another property of int[] type in the existing class and use the linq statement in the getter. Something like item.ChoiceIDs? Commented Dec 16, 2013 at 21:36
  • 1
    SelectMany is not going to help you here. You need to make a new class with choices defined as int[] instead of IList<Item> Commented Dec 16, 2013 at 21:52

2 Answers 2

4

SelectMany flattens List of Lists and produces single List with all items.

items.SelectMany(x=>x.Choices.Select(y=>y.Id).ToArray()));
Sign up to request clarification or add additional context in comments.

Comments

1

get an error about not being able to convert to type int[] from IList how can I cast the property?

You cannot cast one type to another. Instead you can create another property of type int[] in your class and use the LINQ statement in getter (with necessary validation checks).

public int[] ChoiceIDs
{ 
    get {
     return this.Choices.Select(x => x.Id).ToArray();
    }
}

Comments

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.