我有這樣的 ac# 字典:

我需要做的是將字典鍵包含“UPC”的值組合成一個具有組合值的新鍵,因此它的內容如下:{[UPC, 000000000333, 789787878999]}
UPC 可以包含一個或多個值。
我想出了下面的代碼來首先從資訊字典中洗掉可能的重復項

//Remove any duplicates
List<string> vals = new List<string>();
Dictionary<string, string> newDict = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> item in information)
{
if (!vals.Contains(item.Value))
{
newDict.Add(item.Key, item.Value);
vals.Add(item.Value);
}
}
StringBuilder sbUPC = new StringBuilder();
var newDictJustUPCs = newDict.Where(kvp => kvp.Key.Contains("UPC"));
newDictJustUPCs 回傳以下內容:

那么我將如何遍歷這些 KeyValuePairs 并為具有鍵“UPC”和兩個值組合的 newDict 創建 KeyValue 對并將其分配給 newDict?
我知道最終答案的一部分將是 newDict.Add("UPC", CombinedUPCValues);
uj5u.com熱心網友回復:
有很多方法可以做到這一點。這里只有一個
給定的
var dict = new Dictionary<string, string>
{
{ "asd", "dfgdfg" },
{ "ghj", "ghj" },
{ "UPC1", "123" },
{ "UPC2", "345" },
{ "UPC3", "567" }
};
用法
var upc = dict
.Where(x => x.Key.StartsWith("UPC"))
.Select(x => x.Value);
var newDict = dict
.Where(x => !x.Key.StartsWith("UPC"))
.ToDictionary(x => x.Key, x => x.Value);
newDict.Add("UPC", string.Join(",", upc));
foreach (var item in newDict)
Console.WriteLine(item.Key " : " item.Value);
輸出
asd : dfgdfg
ghj : ghj
UPC : 123,345,567
uj5u.com熱心網友回復:
首先提取所有 UPC 元素并根據需要創建其值,然后將其附加到沒有 UPC 元素的新陣列中
var dict = new Dictionary<string, string>
{
{"Modified by", "Stuart gill" },
{"Modified on", "11/2/2021" },
{"UPC0", "000000000333" },
{"UPC2", "789789789" },
};
var keyValueWithUpc = dict.Where(kv => kv.Key.StartsWith("UPC")).ToArray();
var upcValue = string.Join(",", keyValueWithUpc.Select(kv => kv.Value));
var result = dict.Except(keyValueWithUpc)
.Append(new KeyValuePair<string, string>("UPC", upcValue))
.ToDictionary(x => x.Key, x => x.Value);
Append 方法:它需要源(要追加的集合)一個陣列而不是一個,只是為了與現有的一個區分,但由于它作為引數,您也可以傳遞單個專案
public static class ExtensionMethods
{
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, params T[] itemsToAppend)
{
return source.Concat(itemsToAppend);
}
}
uj5u.com熱心網友回復:
TheGeneral 用他的代碼示例為我指明了正確的方向。這是我的解決方案。下面傳入日志資訊。
StringBuilder sb = new StringBuilder();
Dictionary<string, string> information = new Dictionary<string, string>();
List<string> changes = log.Changes.ToString().Split('?').ToList();
changes.RemoveAt(changes.Count - 1);
int start = 0;
foreach (string units in changes)
{
string[] unit = units.Split('|');
string[] parts = unit[0].Split('-');
string[] values = unit[1].Split('^');
string field = parts[1];
string orig = values[0];
if (unit[0] == "VendorPartUpc-UPC")
{
information.Add(field start, values[1]);
start = start 1;
}
else if (values.Count() > 1 && (orig != string.Empty || values[1] != string.Empty) && !information.ContainsKey(field))
information.Add(field, values[1]);
}
//Remove any duplicates
List<string> vals = new List<string>();
Dictionary<string, string> newDict = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> item in information)
{
if (!vals.Contains(item.Value))
{
newDict.Add(item.Key, item.Value);
vals.Add(item.Value);
}
}
//Need to take the UPCs from multiple key/value pairs, extract just the UPC value and put into a single value with UPC as the key.
var newDictJustUPCs = newDict.Where(kvp => kvp.Key.Contains("UPC"));
var upc = newDictJustUPCs
.Where(x => x.Key.StartsWith("UPC"))
.Select(x => x.Value);
newDict.Add("UPC", string.Join(",", upc));
information = newDict;
return information;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347777.html
上一篇:for回圈中字典的賦值
