Merging LINQ result sets with common property at runtime

Suppose I have multiple LINQ result sets that are generated at run-time that have a common key-property but different properties otherwise

Is there anyway, without using Dynamic LINQ (not available on our dev environment), to merge these multiple queries based on the common key-property but without knowing what the properties I'm appending are until run-time?

I also have these same result sets as DataTable objects alternatively - is there perhaps a way to merge these DataTables on the key column and append the additional column values to it?
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
Hi priehl;

C# is a strongly typed language and therefor to do what you want will take some work using Reflections to find out the names of the columns and there data types.

seeming you state, "I also have these same result sets as DataTable objects", it would be much easier to use the DataTable.Merge method to combine them. Make sure that in the data table you have the primary key defined in both tables then you can do this:

table1.Merge(table2, false, MissingSchemaAction.Add);

This will merge table2 into table1.

The following link is the documentation for Merge.
DataTable.Merge Method (DataTable, Boolean, MissingSchemaAction) 

You could do a simple switch statement with all possibilities written out...
priehlAuthor Commented:
A switch statement isn't ideal because there are several arbitrary permutations and combinations in which the resultsets can be merged based on user-input - would prefer a more generic solution
priehlAuthor Commented:
Thanks, that'll work.

Dynamic LINQ would obviously solve this problem in a few lines.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.