【问题标题】:How to get specific value of an dictionary如何获取字典的特定值
【发布时间】:2015-06-11 06:09:57
【问题描述】:

我有一个 int 类型的字典,myclass。 在 myclass 中,我具有以下属性:字符串、int 和字符串数组。

当我将数据保存在字典中时,一切正常。 但是我现在想在字典中查找,如果在 myclass 中的位置字符串数组中有一个值。并过滤它们。例如,数组“German”中包含的每个对象都应该被过滤掉。 有没有可能这样做?

我的班级:

  public class MYCLASS
    {
        public int ID
        {
            get;
            set;
        }

        public string Specialty
        {
            get;
            set;
        }

        public string[] Language
        {
            get;
            set;
        }

 public myclass(int id, string specialty, string[] language)
        {
            this.Language= language;
            this.ID= id;
            this.Specialty = specialty;
        }

}

我的字典

Dictionary<int, myclass> objdict= new Dictionary<int, myclass>();

【问题讨论】:

  • 我不明白...为什么每个请求帮助的人都无法在这里提出一个好的问题。那么您的代码在哪里,您尝试了什么,请澄清您的问题并尝试重新表述您的问题。我的天……
  • 我现在看到有些文字被剪掉了。我很抱歉..
  • 发布代码会很好,因为根据您的解释很难理解。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# object dictionary filter


【解决方案1】:

是的,通过字典的值搜索,然后过滤掉数组

var dict = new Dictionary<int, MYCLASS>();
var filteredPairs = from pair in dict
                    where !pair.Value.Language.Contains("German")
                    select pair;

【讨论】:

  • @saaami11 我在 where 条件处添加了一个!,如果是德语则不包括
  • 对不起,我不明白?
  • 啊,是的,我已经删除了它,但我什么也没得到
  • @saaami11 请注意Contains() 区分大小写,请检查您在语言中的值?
  • 是的,我已经检查过了,该值可用。我必须使用 voytek 示例中的 foreach 来完成此操作。这对我有用。但是感谢您对 Eric 的帮助!
【解决方案2】:

使用 LINQ 是这里的最佳选择。如果您想在常规循环中访问您的课程,它将是这样的:

        foreach (KeyValuePair<string, MYCLASS> entry in MyDic)
        {
            // Value is in: entry.Value and key in: entry.Key
            foreach(string language in ((MYCLASS)entry.Value).Language)
            {
                //Do sth with next language...
            }
        }

【讨论】:

  • 如何在字典中添加新条目?像这样,NewDictionary.Add(entry.Key, entry.Value);
【解决方案3】:

如果我正确理解你的问题,如果你有课:

public class Country
{
    public string CountryName { get; set; }
    public int CountryId { get; set; }
    public string[]  States { get; set; }
}

如果你有字典:

 Dictionary<int, Country> dict = new Dictionary<int, Country>()
 {
     {1,new Country(){ CountryId=1,CountryName="Test1", States=new [] {"State1","State2","State3" }}},
     {2,new Country(){ CountryId=1,CountryName="Test2", States=new []{"State11","State21","State31" }}},
     {3,new Country(){ CountryId=1,CountryName="Test3", States=new []{"State12","State2","State31" }}},
     {4,new Country(){ CountryId=1,CountryName="Test4", States=new []{"Stae112","State21","State31" }}},
  };

然后你可以使用Where过滤它:

 IEnumerable<KeyValuePair<int,Country>> enumerable=  dict.Where(x=>x.Value.States.Contains("State21",StringComparer.OrdinalIgnoreCase));

【讨论】:

  • 感谢您的解决方案。我已经尝试过了,在可枚举的情况下我没有任何物品..
  • 尝试不区分大小写的比较:IEnumerable&lt;KeyValuePair&lt;int,Country&gt;&gt; enumerable= dict.Where(x=&gt;x.Value.States.Contains("State21",StringComparer.OrdinalIgnoreCase));
  • 好的,我也试过这个,但仍然没有项目。它适用于 voytek 的 foreach 示例。但是感谢您的帮助!
  • 你没有数据的原因(实际上你有)_是因为IEnumerabledeferred exectution的约束。这意味着 expression 会被评估,直到它的 realized 值是实际需要的。这在Linq 中广泛使用以获得更好的性能。因此,当您在获取IEnumerable 之后执行foreach 时,您现在实际上是在执行表达式并获取值。
【解决方案4】:

您可以使用 LINQ 表达式来查找与您的查询匹配的记录:

var dictionary = new Dictionary<int, MYCLASS>();
var results = dictionary.Where(record => !record.Value.Language.Any("German"));

【讨论】:

  • @sanderats U 不能将 Any 用于数组?
  • 它在 Linqpad 中工作,否则转换为数组以使用 .ToList() 列出
猜你喜欢
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2021-06-29
  • 2020-04-18
  • 2017-01-05
相关资源
最近更新 更多