简体   繁体   English

如何迭代键值对的每个值<string,list<string> > 词典</string,list<string>

[英]How to iterate each value of keyvaluepair<string,List<string>> dictionary

I've got a exercise, which askes me to make dictionary with next Tasks: Methods which Checks two dictionarys with dictionary<string,List<string>> key refers to teacher object,string list refers to subject values.我有一个练习,要求我用下一个任务制作字典:检查两个字典的方法 dictionary<string,List<string>> 键指的是老师 object,字符串列表指的是主题值。 I'm using List because it can have multiple values我正在使用 List 因为它可以有多个值


    Dictionary<string, List<string>> subjectsOfTeachers = new();
    Dictionary<string, List<string>> subjectsOfPupils = new();

Next task was to check, if any values in subjectOfPupils equals to teachers list values.so i did next:下一个任务是检查 subjectOfPupils 中的任何值是否等于教师列表值。所以我接下来做了:

    public List<string> GetTeachers(string pupil)
    {
        var a = new List<string>();
        foreach(KeyValuePair<string,List<string>>kvp in subjectsOfTeachers)
        {
            if(subjectsOfPupils[pupil] == kvp.Value)
            {
                a.Add(kvp.Key);
            }
        }
        return a;

The logic is that it does checks for same values, but it checks for whole list value similarity.逻辑是它确实检查相同的值,但它检查整个列表值的相似性。 Is there any way to access values lists and check it manualy?有没有办法访问值列表并手动检查? sorry for the broken english.抱歉英语不好。

 subjectsOfPupils[pupil] == kvp.Value

Will compare two lists.将比较两个列表。 Lists are reference types , so by default they use reference equality , and that is probably not what you want.列表是引用类型,因此默认情况下它们使用引用相等性,而这可能不是您想要的。 If I understand the task correctly you want the intersection between the two lists.如果我正确理解任务,您需要两个列表之间的交集 Luckily we have a method just for that: Intersect .幸运的是,我们有一个方法可以做到这一点: Intersect That will return all the items that are present in both lists.这将返回两个列表中存在的所有项目。 If we are only concerned if there was any items or not we can use the .Any method.如果我们只关心是否有任何项目,我们可以使用.Any方法。

If the task is to check if any of the teachers have any subject shared with any pupil we can use SelectMany to flatten the dictionaries to to plain lists.如果任务是检查是否有任何老师与任何学生共享任何主题,我们可以使用SelectMany将字典扁平化为普通列表。

var anySharedSubject = subjectsOfTeachers.SelectMany(kvp => kvp.Value)
    .Intersect(subjectsOfPupils.SelectMany(kvp => kvp.Value))
    .Any();

Note that this uses Linq methods, and while they are very convenient, you should probably think about how you would write the intersect-method using just loops.请注意,这使用了 Linq 方法,虽然它们非常方便,但您可能应该考虑如何仅使用循环来编写相交方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字典 <string, MyClass> 列出 <KeyValuePair<string, string> &gt;其中KeyValuePair中的值为MyClass.ToString() - Dictionary<string, MyClass> to List<KeyValuePair<string, string>> where the Value in KeyValuePair is MyClass.ToString() 如何正确使用列表上的LINQ查询 <KeyValuePair<string, string> 创建字典 <KeyValuePair<string, string> ,int&gt;? - How to properly use LINQ Query on List<KeyValuePair<string, string> to create Dictionary<KeyValuePair<string, string>, int>? 字典 <string, List <KeyValuePair<string,string> &gt;&gt; - Dictionary<string, List <KeyValuePair<string,string>>> 如何更改列表中KeyValuePair的值作为Dictionary中的值? - How to change value of KeyValuePair in a List as a value in a Dictionary? 解析字典<string, List<KeyValuePair<string, string> &gt;&gt; 使用 LINQ 的对象 - Parse a Dictionary<string, List<KeyValuePair<string, string>>> object using LINQ Linq的“列表” <Dictionary<string, KeyValuePair<string, object> &gt;&gt;”按升序对值进行排序 - Linq for “List<Dictionary<string, KeyValuePair<string, object>>>” order the values by asc 排序列表 <keyValuePair<string,string> &gt; - Sort list<keyValuePair<string,string>> 如何转换清单 <KeyValuePair<string, decimal> &gt;列表=新列表 <KeyValuePair<string, decimal> &gt;(); 到DataTable? - How to convert List<KeyValuePair<string, decimal>> list = new List<KeyValuePair<string, decimal>>( ); to DataTable? 插入 KeyValuePair 的值时如何停止 [] 出现<string, string>进入列表控件? - How do I stop [ ] appearing when inserting the value of a KeyValuePair<string, string> into a list control? 如何获取(字符串,列表)的键值对列表的内部列表值? - How can I get the inner list value of list of keyvaluepair of (string, list)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM