开发者

LINQPad access built-in DataContext in extension methods

开发者 https://www.devze.com 2023-03-19 02:56 出处:网络
Can I access the this object in my extension methods? So far this is what I have: void Main() { IQueryable<DataContextTable> list = DataCont开发者_开发知识库extTables.First().NewMethod(this);

Can I access the this object in my extension methods?

So far this is what I have:

void Main() {

    IQueryable<DataContextTable> list = DataCont开发者_开发知识库extTables.First().NewMethod(this);

}

public static class ExtensionMethods {

    public static IQueryable<DataContextTable> NewMethod(this DataContextTable table, TypedDataContext context) {
        return context.DataContextTables.Where(item => item.SomeProperty == true).AsQqueryable();
    }

}

as you can see I still need to pass the TypedDataContext as parameters to my extension methods. Is there any other way I can do this?


I created a static member of type TypedDataSet and "initialize" it in the Main() function with this.

void Main() {

    ExtensionMethods.Context = this;
    IQueryable<DataContextTable> list = DataContextTables.First().NewMethod(this);

}

public static class ExtensionMethods {

    public static TypedDataSet Context;

    public static IQueryable<DataContextTable> NewMethod(this DataContextTable table) {
        return Context.DataContextTables.Where(item => item.SomeProperty == true);
    }

}


Similar to acermate433s' answer, but In LINQPad 4 I created a static member of type TypedDataContext:

void Main()
{
    MyExtensions.Context = this;
}

public static class MyExtensions
{
    public static TypedDataContext Context { get; set; }
    // your method here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消