linqpad - SubmitChanges 扩展

发布于 2024-12-19 07:57:30 字数 721 浏览 1 评论 0原文

是否可以让 SubmitChanges() 在扩展方法中工作?

我目前有这个:

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() 在这里脱离了上下文。我可以从我的查询中传递什么内容来使用此扩展来使其工作吗?

谢谢, 科汉

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

I currently have this:

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.

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?

Thanks,
Kohan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-12-26 07:57:30

您可以通过将当前上下文 (this) 传递到静态方法中来完成此操作:

您的程序:

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

My Extensions.linq 中:

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

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

Your program:

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

In My Extensions.linq:

static void ConfirmSubmitChanges(DataContext context)
{
    if (MessageBox.Show("Submit Changes?", "OK?", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        context.SubmitChanges();
        "Saved".Dump();
    }
}
-残月青衣踏尘吟 2024-12-26 07:57:30

如果有人想知道,请将“this”传递给扩展程序后。这就是我最终创建的。这很好,因为它还确认了更改的数量。

#region ConfirmSubmitChanges(DataContext dc)
public static void ConfirmSubmitChanges(DataContext dc)
{
    ConfirmSubmitChanges(dc, string.Empty);
}

public static void ConfirmSubmitChanges(DataContext dc, string AdditionalMessage)
{
    ChangeSet set = dc.GetChangeSet();
    var countChanges = 0;
        countChanges += set.Deletes.Count();
        countChanges += set.Inserts.Count();
        countChanges += set.Updates.Count();

    if (countChanges>0) {
        if(!string.IsNullOrEmpty(AdditionalMessage)) { AdditionalMessage = "\n\n(" + AdditionalMessage + ")"; }
        if (MessageBox.Show("Do you really want to save "+ countChanges+" changes?" + AdditionalMessage, "Save all changes?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            dc.SubmitChanges();
        }
    }
}

#endregion

In case anyone wants to know, after passing "this" to the Extension. This is what i created in the end. It's nice because it also confirms the number of changes.

#region ConfirmSubmitChanges(DataContext dc)
public static void ConfirmSubmitChanges(DataContext dc)
{
    ConfirmSubmitChanges(dc, string.Empty);
}

public static void ConfirmSubmitChanges(DataContext dc, string AdditionalMessage)
{
    ChangeSet set = dc.GetChangeSet();
    var countChanges = 0;
        countChanges += set.Deletes.Count();
        countChanges += set.Inserts.Count();
        countChanges += set.Updates.Count();

    if (countChanges>0) {
        if(!string.IsNullOrEmpty(AdditionalMessage)) { AdditionalMessage = "\n\n(" + AdditionalMessage + ")"; }
        if (MessageBox.Show("Do you really want to save "+ countChanges+" changes?" + AdditionalMessage, "Save all changes?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            dc.SubmitChanges();
        }
    }
}

#endregion
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文