2016-07-31 5 views
3

私が扱っている情報システム(SAP Business One)では、すべての文書がSQLテーブルで表されています。例えばC# - const string dictionary

クライアントオーダーのドキュメント:ORDR

請求書のドキュメント:OINV

購入引用:OPRQ

等...ユーザーが1つをクリック

ボタンのうち、SQLテーブルの一部をチェックするexist関数を使用し、このクライアントにシステム内のドキュメントがあるかどうかをチェックする必要があります。 この関数は、このクライアントがシステム内に持つドキュメントを表すテーブルの名前を含む文字列メッセージを返します。

テーブル名をドキュメント名に置き換える関数を書く必要があります。

eample:

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

は、私は、文字列の辞書を使用する必要がありますね

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

と交換する必要があります。どうやってするの?辞書とLINQのを使用して

おかげ

+0

可能な重複[C#文字列の辞書で置き換える](http://stackoverflow.com/questions/1231768/c-sharp -string-replace-with-dictionary) – NikolayKondratyev

+0

再コンパイルせずにカスタマイズする必要がありますか? –

+0

実際にテキストを置き換える必要がありますか、「ID:5634には文書:OINV、ORDR」というクライアントを生成していますか? –

答えて

1

var databases = new Dictionary<string, string>(); 

databases["OINV"] = "Invoice"; 
databases["OPRQ"] = "Purchase Quotation"; 
databases["ORDR"] = "Order"; 
// ... 

var str = "Some random text ORDR more text ORDR text OPRQ text OINV text ORDR"; 

var newstr = databases.Aggregate(str, (current, value) => 
    current.Replace(value.Key, value.Value)); 

あなたの辞書を作成した後、後者でも使用することができます。

var str2 = new StringBuilder(str); 

foreach (var pair in databases) { 
    str2.Replace(pair.Key, pair.Value); 
} 

var newstr = str2.ToString(); 
5

は、理想的には、文字列をやってするべきではありません代わりに、生成された文字列で置き換える - 代わりに、翻訳された文字列から生成します。ですから、例えば - あなたが実際に持っているもののコードを知らなくても - あなたが持っている可能性があり:

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 
} 
関連する問題