Rayshawn
Rayshawn

Reputation: 2617

How to cross join deserialization json data in C#

I have two lists of deserialized json data, and I want to cross join the tables. The example data is item1:name, item2:date for both lists. I have the return type as List but my return type is not valid. Not sure what I am doing wrong here.

What I have so far:

 List<JsonData> jdOne = GetDeserializedData(jsonUrlOne);
 List<JsonData> jdTwo = GetDeserializedData(jsonUrlTwo);

 var query = from urlOne in jdOne
                    from urlTwo in jdTwo
                    select new { urlOne, urlTwo };

 return query;

Upvotes: 1

Views: 284

Answers (3)

Alex A
Alex A

Reputation: 148

Looks like you trying to return anonymous type from function. Declare type to hold both values, and return it

select new YourType(){Field1 = urlOne, Field2 = urlTwo}

Upvotes: 1

Bob Vale
Bob Vale

Reputation: 18474

You probably want to use a Tuple to do this....

public List<Tuple<JsonData,JsonData>> myFunc() {
  List<JsonData> jdOne = GetDeserializedData(jsonUrlOne);   
  List<JsonData> jdTwo = GetDeserializedData(jsonUrlTwo);   

  var query = from urlOne in jdOne   
                from urlTwo in jdTwo   
                select Tuple.Create(urlOne, urlTwo);   

  return query.ToList();
}

Then with your results you can access the two items like so.

var results=myFunc();
var x1=results[0].Item1;
var x2=results[0].Item2;

x1 contains the first pair from the first list result, x2 contains the second pair.

Upvotes: 4

cuongle
cuongle

Reputation: 75306

Is this what you want by using Linq: Zip

var result = jdOne.Zip(jdTwo, (urlOne, urlTwo) => new {urlOne, urlTwo});

Upvotes: 1

Related Questions