0

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." 

1 Answer 1

2

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.

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

4 Comments

Thanks. So when I look at the code behind the partial classes in the datacontext, I see things like Partial Class TableA... I should change that to something like Partial Class TableA Implements MyInterface?
No, in a separate file, create your own Partial Class TableA Implements MyInterface. The idea of partial classes is that a single class can be made up of multiple parts.
John, I tried adding an interface and I'm getting a compiler error "Late binding operations cannot be converted to an expression tree." Maybe if the object type is assigned at runtime (because you are querying from an object that implements an interface), the linq cant convert this to a sql query? Dim datasource as IlinqDatatable ; From l in datasource where l.exported=false select l //raises this error
What line of code are you getting that error on. Not in the declaration, I bet, but rather on a LINQ statement. Perhaps you're using a cast incorrectly?

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.