如何从两个列表中进行分组?

编程入门 行业动态 更新时间:2024-10-27 02:23:11
本文介绍了如何从两个列表中进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个IEnumerable,一个具有必备"键基,另一个具有许多具有各种拼写方式的键(相同的值,但包含转义序列,大写字母等).我想在第一个列表的每个键到与第二个列表中的字符串对应的所有键之间建立一个映射(谓词为bool IsPossibleMatch(string a, string b))

I have two IEnumerable, one with a "Must-have" base of keys, the other with a lot of keys with all kinds of spellings (the same values but containing escape-sequences, different capitalization, etc). I want to create a mapping between every key of the first List to all the ones that correspond with the strings in the second List (Predicate is bool IsPossibleMatch(string a, string b))

所以基本上我想要一个IGrouping<string, IEnumerable<string>>,因为那正是我要寻找的东西的结构.

So basically I want an IGrouping<string, IEnumerable<string>> because that's exactly the structure of thing I'm looking for.

我尝试了keys.GroupBy((x,y) => IsPossibleMatch(x, y))的各种版本,但其中的任何一个都无法正常工作.

I tried various versions of keys.GroupBy((x,y) => IsPossibleMatch(x, y)) but couldn't get any of them to work.

现在我想知道:分组依据是否有可能?还是我需要以其他某种方式手动执行此操作?

Now I'm wondering : Is this even possible with group by? Or do I need to manually do this in some other kind of way?

推荐答案

以下是示例代码:

var left = new string[] { "key1", "key2", "key3", "key4", "key5" }; var right = new string[] { "key1", "Key1", "KEy1", "KEY1", "KeY1", "kEY1", "kEy1", "key2", "Key2", "KEy2", "KEY2", "KeY2", "kEY2", "kEy2" }; var output = left.GroupJoin( right, leftStr => leftStr.ToLower(), rightStr => rightStr.ToLower(), (x,y)=>(x,y) ); foreach (var leftKey in output) Console.WriteLine(leftKey.x + "->" + string.Join(",", leftKey.y));

产生以下输出:

key1->key1,Key1,KEy1,KEY1,KeY1,kEY1,kEy1 key2->key2,Key2,KEy2,KEY2,KeY2,kEY2,kEy2 key3-> key4-> key5->

更多推荐

如何从两个列表中进行分组?

本文发布于:2023-07-11 09:30:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!