I have a number of tables which I need to combine into one structure using Linq
I need to process a number of tables that have the same structure but different table names (financial data spread across 20 years). I can create code to access the various table contents dynamically:
string tableName = "Trading.DataSources.Prices2017";
var results = DbContext.Set(Type.GetType(tableName));
However, when I try to Cast the results from one table into a common table (Price which has the same table structure) by using Cast:
var newResults = results.AsQueryable().Cast<Price>().ToList();
I get the following error:
"System.NotSupportedException: 'Unable to cast the type 'Trading.DataSources.Prices2017' to type 'Trading.DataSources.Price'. LINQ to Entities only supports casting EDM primitive or enumeration types.'"
I can obviously do this casting with my own conversion method. However, this results in multiple version of the same block of code to cater for the different tables and every year, when I have a new set of prices data, I have to amend my code to cater for the new year's table name.
Is there a way to deal with this dynamically (or generically)?