我在字典中有两个值,但是当我尝试在循环外获取两个值时,我只得到一个值。 locationdesc变量值被覆盖。有没有更好的方法来创建唯一的变量来处理这个问题
有两个键location-1和location-2。我试图找出如何在循环外得到两个值。我做错了吗?
string locationDesc = "";
string locationAddress = "";
int count = dictionary.Count(D => D.Key.StartsWith("location-"));
for (int i = 1; i <= count; i++)
{
if (dictionary.ContainsKey("location-"+i))
{
string locationData = dictionary["location-"+i];
string[] locationDataRow = locationData.Split(':');
locationDesc = locationDataRow[0];
locationAddress = locationDataRow[1];
}
}
// Only getting location-2 value outside this loop since locationDesc is not unique.
Debug.WriteLine("Location Desc from dictionary is : " + locationDesc);
Debug.WriteLine("Location Add from dictionary is : " + locationAddress);
What I would like to get here is get both the values like locationDesc1 and locationDesc2 instead of locationDesc
我正在寻找的是创建locationDesc和locationAddress唯一,这样我就可以访问for循环之外的两个值。
更多解释因为我不太清楚:
我有一个将在前端创建的动态表。每次创建位置时,我都会创建一个cookie。对于例如location-1,location-2 ... location-n,其中位置描述和位置值为cookie中的值。我试图通过创建一个字典来在后端访问这些值,这样我就可以将所有值分配给唯一变量,这将使我更容易将这些值传递给api调用。我认为我过于复杂化一个简单的问题,可能做错了。
我的api电话会是这样的:
<field="" path="" value=locationDesc1>
<field="" path="" value=locationDesc2>
答案 0 :(得分:2)
循环问题在于您依赖于字典中与循环中的索引匹配的条目的位置。你的第一行代码几乎有它:
int count = dictionary.Count(D => D.Key.StartsWith("location-"));
这告诉我的是,您正在寻找字典中所有以“location-”开头的条目。那么为什么不直接这样做呢?
var values = dictionary.Where(d => d.Key.StartsWith("location-"));
同时进行提取/字符串拆分:
var values = dictionary
.Where(d => d.Key.StartsWith("location-"))
.Select(d => d.Item.Split(':')
.Select(s => new
{
LocationDesc = s[0],
LocationAddress = s[1]
});
这将为您提供一个可以循环的IEnumerable LocationDesc / LocationAddress对:
foreach(var pair in values)
{
Debug.WriteLine(pair.LocationDesc);
Debug.WriteLine(pair.LocationAddress);
}
答案 1 :(得分:0)
试试这个:
int count = dictionary.Count(D => D.Key.StartsWith("location-"));
Dictionary<string, string> values = new Dictionary<string, string>();
for (int i = 1; i <= count; i++)
{
if (dictionary.ContainsKey("location-"+i))
{
string locationData = dictionary["location-"+i];
string[] locationDataRow = locationData.Split(':');
values.Add(locationDataRow[0],locationDataRow[1]);
}
}
foreach (var item in values)
{
Debug.WriteLine(item.Key + " : " + item.Value);
}
答案 2 :(得分:0)
当您处理多个值时,您应该使用一个容器来存储所有值。
如果您只处理两个唯一值,请使用以下代码。
int count = dictionary.Count(D => D.Key.StartsWith("location-"));
string[] locationDesc = new string[2];
string[] locationAddress = new string[2];
for (int i = 1; i <= count; i++)
{
if (dictionary.ContainsKey("location-"+i))
{
string locationData = dictionary["location-"+i];
string[] locationDataRow = locationData.Split(':');
locationDesc[i-1] = locationDataRow[0];
locationAddress[i-1] = locationDataRow[1];
}
}
for (int i = 0; i <= locationDesc.Length-1; i++)
{
Debug.WriteLine("Location Desc from dictionary is : " + locationDesc[i]);
Debug.WriteLine("Location Add from dictionary is : " + locationAddress[i]);
}
如果未修复唯一值的数量,则使用ArrayList
int count = dictionary.Count(D => D.Key.StartsWith("location-"));
ArrayList locationDesc = new ArrayList();
ArrayList locationAddress = new ArrayList();
for (int i = 1; i <= count; i++)
{
if (dictionary.ContainsKey("location-"+i))
{
string locationData = dictionary["location-"+i];
string[] locationDataRow = locationData.Split(':');
locationDesc.Add(locationDataRow[0]);
locationAddress.Add(locationDataRow[1]);
}
}
for (int i = 0; i < locationDesc.Count; i++)
{
Debug.WriteLine("Location Desc from dictionary is : " + locationDesc[i]);
Debug.WriteLine("Location Add from dictionary is : " + locationAddress[i]);
}
简单的一个。如果您只想使用Debug.WriteLine显示结果,请使用以下代码
int count = dictionary.Count(D => D.Key.StartsWith("location-"));
for (int i = 1; i <= count; i++)
{
if (dictionary.ContainsKey("location-"+i))
{
string locationData = dictionary["location-"+i];
string[] locationDataRow = locationData.Split(':');
Debug.WriteLine("Location Desc from dictionary is : " + locationDataRow[0]);
Debug.WriteLine("Location Add from dictionary is : " + locationDataRow[1]);
}
}
目前无法在Visual Studio中准备代码,因此可能存在一些语法错误。
答案 3 :(得分:0)
很难判断你正在尝试做什么。我不会只是将你已经拥有的对象转移到其他对象中以获得乐趣。如果您只是尝试在循环中公开值以与其他函数一起使用,则可以使用LINQ迭代字典。如果你想要一个特定的值,只需添加一个LINQ表达式。在我相信3.5之后,LINQ应该在任何.NET框架中。
public static void ApiMock(string s)
{
Console.WriteLine($"I worked on {s}!");
}
static void Main(string[] args)
{
var d = new Dictionary<int, string> {
{ 1, "location-1" },
{ 2, "location-2" },
{ 3, "location-3" }
};
d.ToList().ForEach(x => ApiMock(x.Value));
//I just want the second one
d.Where(x => x.Value.Contains("-2")).ToList().ForEach(x => ApiMock(x.Value));
//Do you want a concatenated string
var holder = string.Empty;
d.ToList().ForEach(x => holder += x.Value + ", ");
holder = holder.Substring(0, holder.Length - 2);
Console.WriteLine(holder);
}