如何获得字典的特定值 [英] How to get specific value of an dictionary

查看:53
本文介绍了如何获得字典的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int类型为myclass的Dictionary.在myclass中,我具有以下属性:字符串,整数和字符串数组.

i have an Dictionary of type int, myclass. in myclass i have following attributes: an string, int, and an string array.

当我将数据保存在字典中时,Evrything效果很好.但是我现在想查看字典,如果myclass中的位置字符串数组中有值.并过滤它们.例如,包含在德语"数组中的每个对象都应被过滤掉.有可能这样做吗?

Evrything works fine when i save the data in dictionary. But i want now to look in the dictionary, if there is an value, in myclass, at the position string array. And filter them. For example every object that Contains in the Array "German", should be filtered out. Is there an possibility to do that?

myclass:

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

推荐答案

在这里使用LINQ是最好的选择.如果您想定期循环访问您的班级,将是这样的:

Using LINQ is the best option here. If you want access your class in regular loop, it will be like this:

        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...
            }
        }

这篇关于如何获得字典的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆