在我使用的信息系统 (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/