2016-07-29 38 views
0

我有一个公共方法返回的项目的键和值的集合,我想输出特定的键值。以下是我使用在循环中获取特定的键值

public List<string> GetAll(string itemSystemname) 
{ 
    return itemValues[itemSystemname].Select(x => x.Value).ToList(); 
} 

方法的所有值都打印出来使用的foreach这样

foreach (var item in i.GetAll("FrontPageNewsLarge")) 
{ 
    @item 
} 

OutPut: 
Name, 
address, 
phone, 
city, 
Name, 
address, 
phone, 
city 

和我的愿望的方式来走出价值的是这样的

foreach (var item in i.GetAll("FrontPageNewsLarge")) 
{ 
    @item.getvalue("name") // OR item["ID"] 
} 



OutPut: 
Name, 
Name 

ItemValues像这样开始。

public class ItemRepository 
{ 
    public Dictionary<string, HashSet<KeyValuePair<string, string>>> itemValues = new Dictionary<string, HashSet<KeyValuePair<string, string>>>(); 

    public ItemRepository(ItemCollection items) 
    { 
     // populate fields 

     foreach (var itemnames in items) 
     { 
      if (!itemValues.ContainsKey(itemnames.SystemName)) 
       itemValues.Add(itemnames.SystemName, new HashSet<KeyValuePair<string, string>>()); 

      var nameList = itemnames.Names.ToList(); 
      var valueList = itemnames.Values.ToList(); 

      for (int i = 0; i < itemnames.Names.Count(); i++) 
      { 
       if (valueList[i] != null && nameList[i] != null) 
       { 
        var name = nameList[i].ToString(); 
        var value = valueList[i].ToString(); 
        itemValues[itemnames.SystemName].Add(new KeyValuePair<string, string>(name, value)); 
       } 
      } 
     } 
    } 

    public string Get(string itemSystemname, string fieldnames) 
    { 
     if (itemValues != null && itemValues[itemSystemname] != null && itemValues[itemSystemname].Any(f => f.Key == fieldnames)) 
     { 
      return itemValues[itemSystemname].FirstOrDefault(f => f.Key == fieldnames).Value; 
     } 
     return string.Empty; 
    } 

    public List<string> GetAll(string itemSystemname) 
    { 
     return itemValues[itemSystemname].Where(item => item.Key == "Id").ToList(); 
    } 
} 

如何实现此功能。由于提前

+2

你可以显示'itemValues'是什么吗? –

+0

'@ item'和'item'有什么区别? – HimBromBeere

+0

@HimBromBeere - 我认为它可能是@符号的剃刀语法 –

回答

2

更新后:itemValues是从类型:Dictionary<string, HashSet<KeyValuePair<string, string>>>

HashSet的未实现[]操作,所以你不能做到这一点,你想要的方式。你应该做的是:

itemValues["FrontPageNewsLarge"].Where(item => item.Key == "ID") 

//For when you get the field from a parameter and what to output only the "value" 
itemValues[itemSystemname].Where(f => f.Key == fieldNames).Select(f=> f.Value) 

或更改itemValues为类似波纹管


假设itemValues是一样的东西Dictionary<string, Dictionary<string, int>> itemValues = new Dictionary<string, Dictionary<string, someObject>>

然后,只需:

public Dictionary<string, someObject> GetAll(string itemSystemname) 
{ 
    return itemValues[itemSystemname]; 
} 

foreach (var item in i.GetAll("FrontPageNewsLarge")) 
{ 
    item["ID"]; 
} 

但对于那为什么使用一个函数?只写:

foreach(var item in itemValues["FrontPageNewsLarge"]) 
{ 
    var doSomethingWith = item["ID"]; 
} 

(只是确保“FrontPageNewsLarge”和“ID”实际存在于词典)

什么,你也可以做,如果你需要的所有ID:

itemValues["FrontPageNewsLarge"].Select(item => item["ID"]); 
+0

我试了你的建议,但它没有为我工作 –

+0

@UmarKhan - 你是什么意思_didn't work_?你有没有编译错误/异常/空集合? –

+0

是的它它返回了我的编译错误无法隐式转换类型'System.Collections.Generic.List >到'System.Collections.Generic.List ' –