Combining two lists with C # diff structures

I have a list of objects with a structure

string source, string target, int count 

Sample data:

 sourcea targeta 10 sourcea targetb 15 sourcea targetc 20 

My other list of objects with structure

 string source, int addnvalueacount, int addnvaluebcount, int addnvalueccount 

Sample data:

 sourcea 10 25 35 

I want to change the second list to the first list structure, and then make concat all the first list.

Thus, the result should look like this:

 sourcea targeta 10 sourcea targetb 15 sourcea targetc 20 sourcea addnlvaluea 10 sourcea addnlvalueb 25 sourcea addnlvaluec 35 

All help is truly appreciated.

thanks

+6
source share
1 answer

I suggest Concat with SelectMany ; giving you

 List<A> listA = new List<A> { new A ("sorcea", "targeta" , 10), new A ("sorcea", "targetb" , 15), new A ("sorcea", "targetc" , 20), }; List<B> listB = new List<B> { new B ("sourcea", 10, 15, 35), }; 

To Concat all you have to do is add SelectMany :

 var result = listA .Concat(listB .SelectMany(item => new [] { // turn single B item into three A new A(item.source, "addnvaluea", item.addnvalueacount), new A(item.source, "addnvalueb", item.addnvaluebcount), new A(item.source, "addnvaluec", item.addnvalueccount), })); 
+8
source

Source: https://habr.com/ru/post/1013324/


All Articles