tags:

views:

40

answers:

2

I need to do something like this,

My two tables have the same signature, but different class so It suppose to work but it is not working.

var myTable;

if (booleanVariable == true)
{
     myTable = table1;
}
else
{
     myTable = table2;
}


var myLinq1 = from p in myTable
          join r in myOtherTable
          select p;

In this case, I have to initialize myTable

I have tried also,

var myTable= table2;

if (booleanVariable == true)
{
     myTable = table1;
}

var myLinq1 = from p in myTable
          join r in myOtherTable
          select p;

then var is type table2, then it can't be changed to table1 type.

I need help, I don't want to make a copy paste of all the code. the linq query is huge, and it s nested with 5 or 6 queries. also I have to do this on 12 different methods.

Thanks a lot for your help.

A: 

Don't know if this will work, never tried it, but...

If both classes can implement the same interface or share a base class, could you maybe do:

var q = (booleanVariable) 
    ? from p in myTable1 select (ISomeInterface)p 
    : from p in myTable2 select (ISomeInterface)p;

If you use this often, you can put this into its own method - I believe it will return IQueryable<ISomeInterface>.

Then connect to the rest of the LINQ query using the LINQ methods instead of the LINQ C# syntax (i.e. OrderBy() instead of orderby). I just don't know if LINQ-to-SQL will be smart enough to translate this into the correct SQL query.

But I agree with Jon's comment - this is probably bad design, and should be revisited. If you can't reorganize the tables themselves, and if you only need read access, how about a view that unions the two tables together, then tie that view into your LINQ-to-SQL structure.

Joe Enos
This items conteins Dates, and those dates changes states. I have to find the latest time that the state was changed. it made my queries horrible, because the latest changed not necessarily is the latest change on certain column. I am going to try your method and rethink about the design, when more than one people think the same I have to check. thanks four your answer.
Diana
It worked for the first query but in the second nested query the same error appears, there is not implicit conversion between type table1 and type table 2
Diana
thanks a lot Joe, I am changing my sql. I had a mistake, but I think I am on time.
Diana
A: 

I am redesigning my Database. thanks for your answers and comments

Diana