本文介绍了linqpad-SubmitChanges扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从扩展方法中使SubmitChanges()正常工作?

Is it possible to get SubmitChanges() to work from within an extension method?

我目前有这个:

void Main()
{
    // Write code to test your extensions here. Press F5 to compile and run.
    Helper.ConfirmSubmitChanges();
}

public static class Helper
{
    // Write custom extension methods here. They will be available to all queries.
    public static void ConfirmSubmitChanges()
    {
        if (MessageBox.Show("Save?", "Do you really want to save all changes?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            SubmitChanges();
        }
    }
}

// You can also define non-static classes, enums, etc.

但是SubmitChanges()在这里没有上下文.我可以通过查询使用此扩展名使它正常工作吗?

But SubmitChanges() is out of context here. Is there anything that i can pass it from my queries that will use this extension to make it work?

谢谢,可汗

推荐答案

您可以通过将当前上下文(this)传递到静态方法中来实现:

You can do it with the by passing the current Context (this) into the static method:

您的程序:

void Main()
{
    //Do Stuff
    ConfirmSubmitChanges(this);
}

我的扩展程序.linq 中:

static void ConfirmSubmitChanges(DataContext context)
{
    if (MessageBox.Show("Submit Changes?", "OK?", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        context.SubmitChanges();
        "Saved".Dump();
    }
}

这篇关于linqpad-SubmitChanges扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 22:25