Reputation: 18746
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?
Upvotes: 1
Views: 134
Reputation: 43087
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.
Upvotes: 2