LINQ - 向数据上下文添加函数
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切勿编辑 .designer.cs;相反,添加一个第二文件,并使用
partial class
添加方法,例如:No;你不能将其添加到 get 中。不过,另一种选择是扩展方法:
现在您可以使用:
我个人会重命名它以阐明更改的方向,并且有两种方法 - 一种用于编码,一个要解码的。
对一组数据重新执行此操作;也许:
Never edit the .designer.cs; instead, add a second file, and use
partial class
to add the method, for example:No; you can't add this to the get. Another alternative, though, is an extension method:
Now you can use:
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:
也许你可以将你的变量初始化为:
probably you could initialize your variable as: