LINQ - 向数据上下文添加函数

发布于 2024-11-15 17:22:06 字数 677 浏览 2 评论 0原文

我有一个 linq 表“KUND”,它对我来说是只读的。它有一些特殊字符,我编写了一个函数将它们切换为我想要的字符。

public static string changeSpecialCharacters(string kund)
    {
        StringBuilder b = new StringBuilder(kund);

        b = b.Replace("Õ", "å");
        b = b.Replace("┼", "Å");
        b = b.Replace("õ", "ä");
        b = b.Replace("─", "Ä");
        b = b.Replace("÷", "ö");
        b = b.Replace("Í", "Ö");
        b = b.Replace("'", " ");
        b = b.Replace("¦", "´");
        b = b.Replace("Ï", "Ø");

        return b.ToString();
    }

我现在有两个问题:

1 我可以将此函数添加到自动生成的数据上下文中的 GET 中,这样我就不必在我的代码中调用它吗?我已经添加了它,但每当我更改数据上下文(添加/删除表)时,它似乎都会被删除。 2 有什么建议可以使该功能在速度方面更好吗?

I have a linq table "KUND" who is read only to me. It has some special characters in it to which i have writter a function to switch them out to the ones i want.

public static string changeSpecialCharacters(string kund)
    {
        StringBuilder b = new StringBuilder(kund);

        b = b.Replace("Õ", "å");
        b = b.Replace("┼", "Å");
        b = b.Replace("õ", "ä");
        b = b.Replace("─", "Ä");
        b = b.Replace("÷", "ö");
        b = b.Replace("Í", "Ö");
        b = b.Replace("'", " ");
        b = b.Replace("¦", "´");
        b = b.Replace("Ï", "Ø");

        return b.ToString();
    }

I now have two questions:

1 Can i add this function to the GET in the autogenerated datacontext so i dont have to call it all over my code? Ive added it but it seems to be deleted whenever i change how my datacontext is (add/remove table). 2 Any suggestions how to make that function better in regards to speed perhaps?

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

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

发布评论

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

评论(2

马蹄踏│碎落叶 2024-11-22 17:22:06

切勿编辑 .designer.cs;相反,添加一个第二文件,并使用partial class添加方法,例如:

namespace Your.Namespace
{
    partial class YourDataContext
    {
        // your methods here
    }
}

No;你不能将其添加到 get 中。不过,另一种选择是扩展方法:

namespace Some.Utility.Namespace
{
    public static class SomeUtilityClass
    {
        public static string ChangeSpecialCharacters(this string kund)
        { ... } // note the "this" in the above line
    }
}

现在您可以使用:

string name = obj.Name.ChangeSpecialCharacters();

我个人会重命名它以阐明更改的方向,并且有两种方法 - 一种用于编码,一个要解码的。


对一组数据重新执行此操作;也许:

public static IEnumerable<SomeType> ChangeSpecialCharacters(
    this IEnumerable<SomeType> items)
{
    foreach(var item in items)
    {
        item.Name = item.Name.ChangeSpecialCharacters();
        item.Foo = item.Foo.ChangeSpecialCharacters();
        ...
        item.Bar = item.Bar.ChangeSpecialCharacters();
        yield return item;
    }
}

Never edit the .designer.cs; instead, add a second file, and use partial class to add the method, for example:

namespace Your.Namespace
{
    partial class YourDataContext
    {
        // your methods here
    }
}

No; you can't add this to the get. Another alternative, though, is an extension method:

namespace Some.Utility.Namespace
{
    public static class SomeUtilityClass
    {
        public static string ChangeSpecialCharacters(this string kund)
        { ... } // note the "this" in the above line
    }
}

Now you can use:

string name = obj.Name.ChangeSpecialCharacters();

personally I would rename this to clarify the direction of the change, and have two methods - one to encode, one to decode.


Re doing this for a set of data; perhaps:

public static IEnumerable<SomeType> ChangeSpecialCharacters(
    this IEnumerable<SomeType> items)
{
    foreach(var item in items)
    {
        item.Name = item.Name.ChangeSpecialCharacters();
        item.Foo = item.Foo.ChangeSpecialCharacters();
        ...
        item.Bar = item.Bar.ChangeSpecialCharacters();
        yield return item;
    }
}
苹果你个爱泡泡 2024-11-22 17:22:06

也许你可以将你的变量初始化为:

private string kund;
public string Kund
{
    get
    {
        return changeSpecialCharacters(string kund);
    }
    set
    {
        kund = value;
    }
}

probably you could initialize your variable as:

private string kund;
public string Kund
{
    get
    {
        return changeSpecialCharacters(string kund);
    }
    set
    {
        kund = value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文