tags:

views:

247

answers:

1

Hi Guys,

I have 2 tables in a DB with very similar schemas that need to be queried in similar ways. I created a partial class for the linq entities, and then made the classes implement an interface, IEvent which defines all the properties with matching signatures that I need. All is good with that, I can cast my results to IQueryable and use the same code to work on data from more than one source. What I cannot figure out is a nice way to get the Table based on whatever "DataSource" (i.e. event table) is currently active, so currently I have this nasty switch statement that will need updating if another data source gets added and I don't like it one bit. Anyone got any smart ideas, it is late here and my brain is failing... :-S

The code goes 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;
            }
        }
+2  A: 

I would suggest making the method generic and using 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>();
}
jrummell
Hi, this looked like just what I was looking for but when I tryed to implement it I got the following error in VS "T must be a reference type in order to use it as a parameter 'TEntity'". Constraining to a class like Event1 works, but that kind of defeats the purpose. :(
withakay
I added a class constraint to T. This means that T can be any class that implements IEvent.
jrummell
Hi, thanks for taking the time to answer, this is all very handy stuff to know and strictly speaking you have answered my question so I am going to give you credit. I am still left with the problem of deciding what T should be in the first place. Say I want to pull the DataSource from a config file. I was avoiding it, but I am thinking reflection might be a better fit for this problem.
withakay
Hi,I came up with what I think is a simpler solution to my problem. I created a view and used UNION on the (currently) two tables and added a Source column, dragged them onto the Linq designer and wired up the associations. Now I can query and filter based on the source and cast the results to my IEvent interface so the whole thing plugs in quite nicely. I have no requirement to update the data pulled from the view (famous last words...) so this approach looks good from where I am standing. Thanks for you time jrummell
withakay
Your welcome. If the data will never change, then a view would be the best solution.
jrummell