3

I have a list and an array that I would like to return, but I'm unsure how to bring the two together. I have deserialized my JSON and created two objects. How do I bring this list and array together in a single object? :

var one = JsonConvert.DeserializeObject<MyData.RootObject>(first);
var two = JsonConvert.DeserializeObject<MyData.RootObject>(second);

List<myData.DataOne> listOne = new List<myData.DataOne>();

foreach (var items in one) 
{
     someDataModel model = new someDataModel();
     model.property = one.rows.f[0].v;
     listOne.Add(model);
}

string[] array = new string[two.rows.Count];

        for (var items = 0; items < two.rows.Count; items++)
        {
            array[items] = two.rows[items].f[0].v;
        }

return null;

2 Answers 2

3

Create a new class to represent the combination of these two pieces of data:

public class MyReturnType 
{
    public List<myData.DataOne> ListOne {get;set;}
    public string[] Array {get;set;}
}

Then return it:

return new MyReturnType {ListOne = listOne, Array = array};
Sign up to request clarification or add additional context in comments.

3 Comments

Beautiful answer, sir. This gives a wider range of flexibility. My program now works as desired. Many thanks!
@EmpereurAiman: You can't use anonymous types as a return type, so unless this result is just getting serialized as part of a REST service or something, that won't work.
@StriplingWarrior Thanks for correcting me. I got confused seeing the usage in LINQ.
2

Create a Tuple with types as List<> and string[].

var tupleObject = new Tuple<List<myData.DataOne>, string[]>(listOne, array);
return tupleObject;

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.