C# 在循环中获取特定的键值

C# 在循环中获取特定的键值,c#,C#,我有一个公共方法,它返回项目的键和值的集合,我想输出特定的键值。下面是我正在使用的方法 public List<string> GetAll(string itemSystemname) { return itemValues[itemSystemname].Select(x => x.Value).ToList(); } 我想要的方法是这样的 foreach (var item in i.GetAll("FrontPageNewsLarge")) { @item

我有一个公共方法,它返回项目的键和值的集合,我想输出特定的键值。下面是我正在使用的方法

public List<string> GetAll(string itemSystemname) 
{
  return itemValues[itemSystemname].Select(x => x.Value).ToList();
}
我想要的方法是这样的

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
ItemValue是这样启动的

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=新字典();
公共ItemRepository(ItemCollection项目)
{
//填充字段
foreach(项目中的var itemnames)
{
如果(!itemValues.ContainsKey(itemnames.SystemName))
Add(itemnames.SystemName,newhashset());
var nameList=itemnames.Names.ToList();
var valueList=itemnames.Values.ToList();
对于(int i=0;if.Key==fieldnames))
{
返回itemValues[itemSystemname].FirstOrDefault(f=>f.Key==fieldnames).Value;
}
返回字符串。空;
}
公共列表GetAll(字符串itemSystemname)
{
返回itemValues[itemSystemname]。其中(item=>item.Key==“Id”).ToList();
}
}

如何实现此功能。更新后提前感谢:itemValues来自类型:
Dictionary

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 itemValues=newdictionary

那么就:

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

foreach (var item in i.GetAll("FrontPageNewsLarge"))
{
    item["ID"];
}
(只需确保字典中确实存在“FrontPageNewsLarge”和“ID”)

如果需要所有ID,您还可以做什么:

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

更新后:itemValues来自类型:
Dictionary

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 itemValues=newdictionary

那么就:

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

foreach (var item in i.GetAll("FrontPageNewsLarge"))
{
    item["ID"];
}
(只需确保字典中确实存在“FrontPageNewsLarge”和“ID”)

如果需要所有ID,您还可以做什么:

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


你能告诉我什么是
itemValues
吗?以及
@item
item
之间的区别是什么吗?@HimBromBeere-我想这可能是@symbol的剃刀语法,你返回的是一个列表或字符串,而不是字典或列表keyvaluepairs@GiladGreen我已经更新了我的问题,您可以查看项目值扫描您显示了什么
itemValues
是?那么
@item
item
之间有什么区别?@HimBromBeere-我认为可能是@symbol的剃刀语法让您返回一个或多个列表,而不是字典或列表keyvaluepairs@GiladGreen我已经更新了我的问题,您可以查看项目值我尝试了您的建议,但没有成功为我工作Either@UmarKhan-你说的没用是什么意思?您是否收到编译错误/异常/空集合?是的,它返回我编译错误无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”我所理解的是您的建议与此公共字符串get(string itemSystemname,string FieldName)非常相似{return itemValues[itemSystemname].Distinct().FirstOrDefault(f=>f.Key==fieldnames).Value;}这给了我一个键的值,但是如果同一个键有多个值呢?@UmarKhan-请用编辑过的代码编辑你的问题,这样我就可以正确地看到为什么要使用Distinct?或FirstOrDefault(特别是如果你说你想要一组值)1.我试过你的建议,但不管用Either@UmarKhan-你所说的“不工作”是什么意思?你有没有收到编译错误/异常/空集合?是的,它返回给我编译错误无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”我所理解的是这样的与此公共字符串Get(string itemSystemname,string fieldnames)类似的mthing{return itemValues[itemSystemname].Distinct().FirstOrDefault(f=>f.Key==fieldnames).Value;}这给了我一个键的值,但是如果我对同一个键有多个值呢?@UmarKhan-请用编辑过的代码编辑你的问题,这样我就可以正确地看到,但为什么要使用Distinct?或FirstOrDefault(特别是如果你说你想要一组值的话)。