tags:

views:

48

answers:

1

So it appears with a simple designer change I can make the 3 tables implement the same interface without any additional coding, from here is it possible to get the rows to behave as queryable objects of the interface type without pulling the whole table down into memory and row by row cast?

each table can be treated as such:

[Table(Name="dbo.bLog")]
public partial class bLog : IBlogRow

where the interface is:

public interface IBlogRow
{
 String App{get;set;}
 String Version{get;set;}
String Message{get;set;}
DateTime dt{get;set;}
int? value {get;set;}
String ex {get;set;}
int? SecsTaken{get;set;}
bool? loanLevel{get;set;}

}

I think this is a contravariance or covariance problem. I can't seem to cast the table as a system.linq.table nor cast it as IQueryable ? How would I get this working so that i can write the same query code for all 3 tables that have the same design, and use them interchangeably?

+2  A: 

Have you tried something along these lines?

IEnumerable<IBlogRow> GetRows()
{
    DB db = new DB();
    return db.BlogRows.Where(row => row.App == "something").Cast<IBlogRow>();
}

I think you'll have to cast after performing your query since LINQ to SQL won't be able to map IBlogRow properties to SQL table columns.

Update:

You could also try Dynamic Linq. You should be able to reuse most of the query code between the 3 tables. The drawback here is that you would give up strongly typed queries for magic strings.

Another option is using a PredicateBuilder. Take a look at the Generic Predicates example. This looks a lot like what you're describing.

jrummell
the original design here was so that I write linq queries that would work across all three tables, so casting after the query doesn't eliminate the main redundancy. I have conditional filtering across almost every column in the query section... and have to duplicate that query via copy and paste for each linq class.
Maslow
+1 Generic Predicates appear to solve the problem, either way both resources are worthy of note and interesting. I'll accept as soon as I figure it out/test.
Maslow
This doesn't appear to work in Vb.net since it requires explicit implementation, I'd have to put the code in the generated file. I was partially through conversion to C# for the linq code anyway thinking this would be a major pitfall for vb.
Maslow
@Maslow, did you have to convert to C# to get PredicateBuilder working?
jrummell