Search code examples
c#linqcross-join

Merge data into single Dictionary after cross join of two list in c#


enter image description hereI have two Lists of Dictionary and I am applying LINQ query to get cross join.

var result= list1.SelectMany(x=> list2,(x,y)=> new {x,y});

Now the result has all records but both x and y are separated but I want both Dictionaries in a single row without x and y, also Dictionary in tables are dynamic so it may have any key and values.

Thanks in Advance!


Solution

  • Perhaps you mean you want your output "row" to be a single dictionary containing all of x and all of y. It could be like:

    var result= list1.SelectMany(
      x=> list2,
      (x,y) => new Dictionary<WhateverType,YouHave>(x.Concat(Y))
    );
    

    It takes all of the KeyValuePair<WhateverTypr,YouHave> in x and y and makes an IEnumerable<KeyValuePair<WhateverType,YouHave>> from them, then makes a new Dictionary that contains all those keyvaluepairs

    The types of the dictionary will need to be consistent; you can't have x be a Dictionary<string,int> and y be a Dictionary<int,string> for example