Joe Albahari

A question regarding LINQ to SQL in the January Orcas CTP.

DataContext.GetTable is now internal - what method should we use instead For now I'm calling GetTable via reflection which works fine but is a bit messy.

I notice a number of LINQ to SQL bugs have been fixed which is great.

Thanks

Joe



Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Zhe Fu

There are two public GetTable methods in DataContext class as follows.

public Table<TEntity> GetTable<TEntity>()

public ITable GetTable(Type type)

For example, if dc is the data context and Customer is an entity class for a table in the data context. You can get the table object as follows.

System.Data.Linq.Table<Customer> customerTable = dc.GetTable<Customer>();





Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Joe Albahari

Hi Zhe

The two methods you describe were public in the May CTP, but are internal in the Jan CTP.

Joe





Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Keith Farmer

Looking at the current source code, Zhe is correct -- GetTable<TEntity>() and GetTable(Type) are both public. I don't know if these would be made internal during a CTP release (eg, the codepaths are too un-tested, or some similar reason).






Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Joe Albahari

Hi Keith

GetTable is most definitely internal in the Jan CTP.

Just as well for System.Reflection :). I've ported a test project to the Jan CTP and ran about 100 unit tests comprising various queries, some of which failed uner the May CTP. Under the Jan CTP, there are no new LINQ to SQL bugs and most May CTP bugs have been fixed. So it's looking good.

Joe




Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Keith Farmer

There are actually several overloads to GetTable, and some must remain public (GetTable(type), GetTable<TEntity>()) for use in data contexts. What does your data context look like, which works under the Jan bits Our unittests are using, for example:

public System.Data.Linq.Table<Category> Categories
{
get
{
return this.GetTable<Category>(); // this wouldn't compile if GetTable<TEntity>() were internal
}
}

Glad to hear our bug fixes are noticed :) You can thank the QA team for their devious efforts to confound us.






Re: LINQ Project General What's the replacement for GetTable() in DataContext?

David Gardiner

Keith Farmer wrote:

There are actually several overloads to GetTable, and some must remain public (GetTable(type), GetTable<TEntity>()) for use in data contexts. What does your data context look like, which works under the Jan bits



Not sure how your units tests work, but the following are the only "GetTable" methods show up in the Object Browser:

Friend Function GetTable(Of T)() As System.Data.DLinq.Table(Of T)
Member of: System.Data.DLinq.DataContext

Friend Function GetTable(ByVal type As System.Type) As System.Data.DLinq.ITable
Member of: System.Data.DLinq.DataContext


Neither is public as far as I can see.

-david




Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Joe Albahari

Here's a screen dump from .NET Reflector:

http://www.albahari.com/GetTable.png

Joe





Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Matt Warren - MSFT

These functions were made internal because they had not passed the required level of testing to make it into the main source code branch. The next CTP coming soon will rectify this. It should be on par functionality-wise with last May's LINQ preview.






Re: LINQ Project General What's the replacement for GetTable() in DataContext?

fabianus

Hi Matt,

thanks for explenation.

But in mean time, how may we do this without gettable

' DataContext takes a connection string

Dim db As DataContext = _
New DataContext("c:\...\northwnd.mdf")

'Get a typed table to run queries

Dim Customers As Table(Of Customer) = db.GetTable(Of Customer)()

'Query for customers from London

Dim q = _

From c In Customers _

Where c.City = "London" _

Select c

For Each cust In q

Console.WriteLine("id=" & Customer.CustomerID & _

", City=" & Customer.City)

Next

Regards,
Fabian





Re: LINQ Project General What's the replacement for GetTable() in DataContext?

fabianus

Joe Albahari wrote:

For now I'm calling GetTable via reflection which works fine but is a bit messy.

Hi Joe,

how do you do this, could you give us an example

Thanks a lot!

Regards,
Fabian





Re: LINQ Project General What's the replacement for GetTable() in DataContext?

Joe Albahari

Add this method to your DataContext class:

public Table<T> GetTable<T> ()
{
MethodInfo mi = GetType ().GetMethod ("GetTable", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof (Type) }, null);
return (Table<T>)mi.Invoke (this, new object[] { typeof (T) } );
}


Regards

Joe





Re: LINQ Project General What's the replacement for GetTable() in DataContext?

fabianus

Hi Joe,

thanks a lot for your answer. So if I understand it right, you create a class (DataContextHelp) and you inherit DataContext and add this method

I do not know C# and would be very thankfull if someone could help me to translate this into vb.net ...

And one more thing, how did you write the new methode of this DataContectHelp class

Excuse-me for bothering you with this, hope it's ok.

Thanks anyway for your answer above!

Regards,
Fabian