acermate433s
acermate433s

Reputation: 2544

LINQPad access built-in DataContext in extension methods

Can I access the this object in my extension methods?

So far this is what I have:

void Main() {

    IQueryable<DataContextTable> list = DataContextTables.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?

Upvotes: 3

Views: 868

Answers (2)

Colin
Colin

Reputation: 2011

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
}

Upvotes: 1

acermate433s
acermate433s

Reputation: 2544

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

}

Upvotes: 3

Related Questions