c# - 如何用字符串中的对象类型替换表名?

标签 c#

在我使用的信息系统 (SAP Business One) 中,每个文档都在 SQL 表中表示。

Client order document : ORDR

Invoice document : OINV

Purchase Quotation : OPRQ

当用户单击其中一个按钮时,我需要使用一个存在函数来检查部分 SQL 表,并检查此客户端是否在系统中有文档。 该函数返回一个字符串消息,其中包含代表该客户在系统中拥有的文档的表的名称。

我需要编写一个函数,用文档名称替换表名称。

示例:

"Client with ID:5634 has documents: OINV, ORDR"

需要替换为

 "Client with ID:5634 has documents: Invoice, Client order"

我猜应该使用字符串字典。怎么做?

谢谢

最佳答案

理想情况下,您不应该使用生成的字符串进行字符串替换 - 而是从翻译后的字符串生成它。因此,例如 - 在不知道您实际获得的代码的情况下 - 您可以:

private static readonly Dictionary<string, string> TableNameTranslations
    = new Dictionary<string, string>
{
    { "ORDR", "Client order document" },
    { "OINV", "Invoice document" },
    { "OPRQ", "Purchase quotation" }
};

...

public string GetClientDocumentDisplayString(int clientId)
{
    var tableNames = GetTableNamesForClient(clientId);
    var translatedNames = tableNames.Select(t => TableNameTranslations[t]);
    return $"Client with ID:{clientId} has documents: {string.Join(",", translatedNames)}";
}

private IList<string> GetTableNamesForClient(int clientId)
{
    // Whatever your code needs, returning ORDR, OINV etc
}

关于c# - 如何用字符串中的对象类型替换表名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681787/

相关文章:

c# - 远程wmi连接c# - 无效参数错误

c# - 其他语言的Mysql查询

c# - 如果不存在则生成方法

c# - 在指定位置写入(控制台)

c# - Winform 应用程序在将数据上传到数据库时显示超时过期错误

c# - 如何在 gridview 中查找特定行的值?

c# - 访问自定义属性的值

c# - SlowCheetah 没有为 EntityFramework 迁移进行转换

c# - Update-Database 由于未决更改而失败,但 Add-Migration 创建了重复迁移

C#、BitConverter.ToUInt32、不正确的值