简体   繁体   中英

Extension method on datacontext Linq to sql

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>

有可能,但是由于类是局部的,因此您可以简单地将方法添加到其他文件中

Example:

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

    }
}

Something like this should work:

public static IQueryable myExtensionMethod(this DataContext dc)
{
    ...
}

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
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM