1

I have a web server control that is dynamically generated based on a table in my entity framework. Now a decision is being made to have an identically structured table with a different set of data, and I need some way to dynamically repoint my control to the other table. (I have made my case for keeping one table and just adding a flag so lets not go down thatroad please)

I could do this using linq-to-entities, by adding the new table to my entity model, and just conditionally querying one or the other table in the model. The problem is the results returned by the query will be collections of two different entity classes. This poses a problem with all the code that deals with these collections, as it will have to be modified and potentially duplicated to make this work.

So if I can somehow map the results of the linq queries such that regardless of which table I query, the results are of the same class type, then it will minimize the impact on the rest of the code. How would I accomplish something like the below where the two different tables can be materialized as a list of the same class type?

Note that I am using .NET 3.5

var formFooQuery;
if(mode1 == true)
{
        formFooQuery = from r in EntitiesContext.Foo1Set.Include("Foo1Data")
                                where r.FooId == fooId
                                select r;
}
else
{
        formFooQuery = from r in EntitiesContext.Foo2Set.Include("Foo2Data")
                                where r.FooId == fooId
                                select r;
}
List<Foo> foos = formFooQuery.ToList<Foo>();

2 Answers 2

1

You could do this in Entity Framework by utilizing the POCO Support.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I think that will be good solution once they are using 4.0, but am using 3.5 right now.
1
if(mode1 == true)    
{    
        formFooQuery = from r in EntitiesContext.Foo1Set.Include("Foo1Data")    
                                where r.FooId == fooId    
                                select new FooViewModel { FooId = r.FooId, FooName = r.FooName };    
}    
else    
{    
        formFooQuery = from r in EntitiesContext.Foo2Set.Include("Foo2Data")    
                                where r.FooId == fooId    
                                select new FooViewModel { FooId = r.FooId, FooName = r.FooName };    
}    
List<FooViewModel> foos = formFooQuery.ToList<FooViewModel>();    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.