我有一个公共方法返回的项目的键和值的集合,我想输出特定的键值。以下是我使用在循环中获取特定的键值
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();
}
}
如何实现此功能。由于提前
你可以显示'itemValues'是什么吗? –
'@ item'和'item'有什么区别? – HimBromBeere
@HimBromBeere - 我认为它可能是@符号的剃刀语法 –