Is dynamic Linq.Table <TEntity> possible?

I have 2 tables in a database with very similar schemas that need to be requested in the same way. I created a partial class for linq objects, and then made the classes implement the interface, IEvent, which defines all the properties with the corresponding signatures that I need. Everything is good with this, I can bring my results to IQueryable and use the same code to work with data from more than one source. What I can’t understand is a good way to get the table based on the fact that the “DataSource” (ie the event table) is currently active, so currently I have this nasty switch statement that will need updating, if another data source is added, I like it. Someone has smart ideas, it's late here, and my brain fails ...: -S

The code looks something like this:

private IQueryable<IEvent> getEvents(IEnumerable<int> IDs)
        {
            var db = new EventDataContext();
            // activeDataSource is an enum defined elsewhere
            switch (activeDataSource)
            {
                case DataSource.Source1:
                    return db.Events1.Where(e => IDs.Contains(e.ID)).Cast<IEvent>();
                    break;
                case DataSource.Source2:
                    return db.Events2.Where(e => IDs.Contains(e.ID)).Cast<IEvent>();
                    break;
            }
        }
+3
1

DataContext.GetTable():

private IQueryable<IEvent> getEvents<T>(IEnumerable<int> IDs)
    where T : class, IEvent
{
    var db = new EventDataContext();
    return db.GetTable<T>.Where(e => IDs.Contains(e.ID)).Cast<IEvent>();
}
+4

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


All Articles