C#变量未获取循环外部的所有值

C#变量未获取循环外部的所有值,c#,C#,字典中有两个值,但当我尝试在循环外获取这两个值时,我只得到一个值。locationdesc变量值正在被覆盖。有没有更好的方法来创建唯一变量来处理此问题 有两个键位置1和位置2。我试图找出如何将这两个值都放到循环之外。我做错了吗 string locationDesc = ""; string locationAddress = ""; int count = dictionary.Count(D => D.Key.StartsWith("location-")); for (int i

字典中有两个值,但当我尝试在循环外获取这两个值时,我只得到一个值。locationdesc变量值正在被覆盖。有没有更好的方法来创建唯一变量来处理此问题

有两个键位置1和位置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);
试试这个:

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);
}
int count=dictionary.count(D=>D.Key.StartsWith(“location-”);
字典值=新字典();

对于(int i=1;i循环的问题在于,您依赖于字典中条目的位置与循环中的索引相匹配

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);
}

在处理多个值时,应该使用一个可以存储所有值的容器

如果您只处理两个唯一值,请使用下面的代码。

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]);
}    
int count=dictionary.count(D=>D.Key.StartsWith(“location-”);
string[]locationDesc=新字符串[2];
string[]locationAddress=新字符串[2];

对于(int i=1;i很难判断事件试图做什么。我不会只是为了好玩而将您已有的对象转储到其他对象中。如果您只是试图公开循环中的值以用于另一个函数,您可以只使用LINQ在字典中迭代。如果您想要特定的值,只需添加一个where LINQ表达式n、 我认为LINQ应该在3.5之后的任何.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);
}
publicstaticvoidapimock(字符串s)
{
WriteLine($“我在{s}上工作过!”);
}
静态void Main(字符串[]参数)
{
var d=新字典{
{1,“位置-1”},
{2,“位置-2”},
{3,“位置-3”}
};
d、 ToList().ForEach(x=>ApiMock(x.Value));
//我只想要第二个
d、 其中(x=>x.Value.Contains(“-2”)).ToList().ForEach(x=>ApiMock(x.Value));
//是否要连接字符串
var holder=string.Empty;
d、 ToList().ForEach(x=>holder+=x.Value+“,”);
holder=holder.Substring(0,holder.Length-2);
控制台。写线(支架);
}

您到底想做什么?尝试将两个键值都从循环外部获取,以便访问循环外部的值。您已经可以了,这就是循环,您只需要指定要使用的键。您的所有值都已保存在
字典中
为什么不直接执行
var values=dictionary。在哪里(d=>d.Key.StartsWith(“location-”);
?那么就不需要循环了。@kjaquier这个问题和Javascript到底有什么关系?很抱歉没有解释清楚。我已经用更多信息更新了我的问题。
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]);
}
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]);
        }
    }
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);
}