views:

83

answers:

4

Is it possible to create an extension method on the DataContext, not on the table in the datacontext but directly on the dataContext to get dynamicly a table.

ex:

DataContext dc = new DataContext();

var test = from a in dc.myExtensionMethod(args) select a;

ps: I Already know dc.GetTAble and dc.GetTable<T>

+2  A: 

It's possible, but since the class is partial, you could simply add your method in a different file

Vedran
+2  A: 

Example:

namespace System.Data.Linq
{
    public static class DataContextExtensions
    {
        public static bool IsConnected(this DataContext context)
        {
            return (context.Connection.State == ConnectionState.Open);
        }

    }
}
Chris Arnold
Thank, i will give the answer to Chris Arnold, because it's the first one who answer. Thanks
Cédric Boivin
+2  A: 

Something like this should work:

public static IQueryable myExtensionMethod(this DataContext dc)
{
    ...
}
akmad
+2  A: 

I'm not sure what your args is, or if you want to match the signature of GetTable, but...

namespace System.Data.Linq
{
    public static class DataContextExtensions
    {
         public static (Table<T>,IQueryable, whatever) 
            MyExtensionMethod(this DataContext context, Args args)
         {
            //do your magic here
         }
    }
}
Joseph