LINQ - adding a function to a datacontext

I have a linq table "KUND" that is read only to me. It has special characters in which I have a function to switch them 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();
    }

Now I have two questions:

1 Can I add this function to GET in an autogenerated datacontext, so I don’t need to call it all over my code? I added it, but it seems to be deleted whenever I modify my datacontext file (add / delete table). 2 Any suggestions on how to make this feature better in terms of speed, perhaps?

+3
source share
2 answers

.designer.cs; partial class , :

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

; 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;
    }
}
+3

, :

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

All Articles