bernie2436
bernie2436

Reputation: 23921

LINQ to SQL: using multiple data contexts easily with an interface

I have a program that is accessing many similar tables using a linq to sql datacontext. Do linq to sql tables implement a common interface? I would like to write methods that could work with any of the similar tables, so it would be great to do something like

Dim myTable as ILinqDataTable
If switch = "TableA" then myTable=myDataContext.TableA Else myTable=myDataContext.TableB

Is this possible with LINQ to SQL?

Update

Dim datasource as IlinqDatatable  // new interface that I implemented based on John's suggestion
Dim rec =  From l in datasource where l.exported=false select l //raises "Late binding operations cannot be converted to an expression tree." 

Upvotes: 0

Views: 505

Answers (1)

John Saunders
John Saunders

Reputation: 161821

There is no built-in interface that you could use. However, you can create your own.

The classes generated by LINQ to SQL are partial classes. This allows you to create another class part and specify that the class implements an interface. This way, you can force all of the similar tables to implement the same interface.

Upvotes: 2

Related Questions