Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GroupBy键-值对列表,然后重新组合结果_C#_Linq_Grouping - Fatal编程技术网

C# GroupBy键-值对列表,然后重新组合结果

C# GroupBy键-值对列表,然后重新组合结果,c#,linq,grouping,C#,Linq,Grouping,我想按键对键值对列表进行分组,然后对具有相同值列表的所有键进行分组 这里有一个例子 | Key | Value | |:----|------:| | 1 | A | | 1 | B | | 1 | C | | 2 | A | | 2 | B | | 3 | A | 这是我想要的结果 | Keys | Values | |:-----|-------:| | 1,2 | A,B | | 1 | C

我想按键对键值对列表进行分组,然后对具有相同值列表的所有键进行分组

这里有一个例子

| Key | Value |
|:----|------:|
| 1   | A     |
| 1   | B     |
| 1   | C     |
| 2   | A     |
| 2   | B     |
| 3   | A     |
这是我想要的结果

| Keys | Values |
|:-----|-------:|
| 1,2  | A,B    |
| 1    | C      |
| 3    | A      |
我从一个经典的
GroupBy
开始,但是我被困在第二步中,无法对键进行分组

var pairs = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("1", "A"),
    new KeyValuePair<string, string>("1", "B"),
    new KeyValuePair<string, string>("1", "C"),
    new KeyValuePair<string, string>("2", "A"),
    new KeyValuePair<string, string>("2", "B"),
    new KeyValuePair<string, string>("3", "A")
};
var groupingFirstStep = pairs.GroupBy(p => p.Key);

| Key  | Values |
|:-----|-------:|
| 1    | A,B,C  |
| 2    | A,B    |
| 3    | A      |

我想这就是你需要的。我用C省略了
KeyValuePair
,因为我假设您想知道当值(在本例中
A,B
)相同时如何对键进行分组:

var pairs = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("1", "A"),
    new KeyValuePair<string, string>("1", "B"),
    new KeyValuePair<string, string>("2", "A"),
    new KeyValuePair<string, string>("2", "B"),
    new KeyValuePair<string, string>("3", "A")
};
var grouping = pairs.GroupBy(p => p.Key)
  .GroupBy(g => string.Join(",", g.Select(kvp => kvp.Value)));
var pairs=新列表
{
新的KeyValuePair(“1”、“A”),
新的KeyValuePair(“1”、“B”),
新的KeyValuePair(“2”,“A”),
新的KeyValuePair(“2”,“B”),
新的KeyValuePair(“3”、“A”)
};
var grouping=pairs.GroupBy(p=>p.Key)
.GroupBy(g=>string.Join(“,”,g.Select(kvp=>kvp.Value));
您可以使用
.GroupBy
从中投影值

大概是这样的:

static void Main(string[] args)
{
    var pairs = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("1", "A"),
        new KeyValuePair<string, string>("1", "B"),
        new KeyValuePair<string, string>("1", "C"),
        new KeyValuePair<string, string>("2", "A"),
        new KeyValuePair<string, string>("2", "B"),
        new KeyValuePair<string, string>("3", "A")
    };

    IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"{x}\t|\t{string.Join(",", y)}");

    foreach (string groupItem in groupings)
    {
        Console.WriteLine(groupItem);
    }
} 

您的分组没有相同的值列表-它们只是有一些值的交叉点-并且
1,3-A也可能-对于
1,2-A相同-为什么1和2应该分组在一起?1有A、B和C,而2有A和B。@MichałTurczyn可能是因为它们都有A和B,但是为什么没有一组显示出1、2和3有A的共同点呢?@MichałTurczyn,我的目标是尽量减少行数,不管是什么组keys@Nicolas,上一个表的行数与第二个表的行数相同,因此,为什么不坚持这一条呢?它不会给出第二行
1 | C
,如果你省略了C,你会简化太多,重新整合它,你会发现它不起作用。在花时间寻找解决方案后,你的答案似乎是我看到的最好的:根据值列表第二次分组。因此,它只对值的精确匹配进行分组,但总比没有好。谢谢,谢谢你的回答。在结果中,我希望1和2被分组,因为它们都有A和B。
static void Main(string[] args)
{
    var pairs = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("1", "A"),
        new KeyValuePair<string, string>("1", "B"),
        new KeyValuePair<string, string>("1", "C"),
        new KeyValuePair<string, string>("2", "A"),
        new KeyValuePair<string, string>("2", "B"),
        new KeyValuePair<string, string>("3", "A")
    };

    IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"{x}\t|\t{string.Join(",", y)}");

    foreach (string groupItem in groupings)
    {
        Console.WriteLine(groupItem);
    }
} 
1       |       A,B,C
2       |       A,B
3       |       A