3

我有以下数据

IEnumerable<Tuple<T, List<U>>> Data;

或者也可以表示为

Dictionary<T, List<U>> GroupedA

给定一个 T,它可能属于一个或多个 U。我想反转它,以便查找 U 并在结构中找到所有关联的 T

Dictionary<U, List<T>> GroupedB;

试图想出一个简洁的 LINQ 表达式来反转字典。

编辑

实际上下面的正确答案表明我真正想要的是

ILookup<U, T>

而不是

Dictionary<U, List<T>>
4

2 回答 2

6
//Populating data
Dictionary<int, List<string>> GroupedA = new Dictionary<int, List<string>>();

GroupedA.Add(1, new List<string>{"1","2","3"});
GroupedA.Add(2, new List<string>{"1","32","3","4"});
GroupedA.Add(3, new List<string>{"1","52","43","4"});


//Inverting data
ILookup<string, int> GroupedB = 
       GroupedA.SelectMany(pair => pair.Value.Select(val => new{pair.Key, val}))
               .ToLookup(pair => pair.val, pair => pair.Key);



//Printing data 
var pairs = GroupedB.Select(pair => string.Format("{0} : {1}", pair.Key, string.Join(",", pair)));

Console.WriteLine (string.Join(Environment.NewLine, pairs));

印刷:

1 : 1,2,3 
2 : 1 
3 : 1,2 
32 : 2 
4 : 2,3 
52 : 3 
43 : 3 
于 2013-05-28T07:42:17.093 回答
4

尝试这个:

Data.SelectMany(tuple => tuple.Item2.Select(u => new { U = u, T = tuple.Item1 }))
    .GroupBy(x => x.U)
    .ToDictionary(g => g.Key, g => g.Select(x => x.T).ToList());

完整的测试用例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;

namespace StackOverflow
{
    public class Class1
    {
        //Populating data
        Dictionary<int, List<string>> GroupedA = new Dictionary<int, List<string>>();

        public Class1()
        {
            GroupedA.Add(1, new List<string> { "1", "2", "3" });
            GroupedA.Add(2, new List<string> { "1", "32", "3", "4" });
            GroupedA.Add(3, new List<string> { "1", "52", "43", "4" });
        }

        [Fact]
        public void ToDictionarySpec()
        {
            var data = GroupedA.Select(v => Tuple.Create(v.Key, v.Value));

            var r = data.SelectMany(tuple => tuple.Item2.Select(u => new { U = u, T = tuple.Item1 }))
            .GroupBy(x => x.U)
            .ToDictionary(g => g.Key, g => g.Select(x => x.T).ToList());

            //Printing data 
            var pairs = r.Select(pair => string.Format("{0} : {1}", pair.Key, string.Join(",", pair.Value)));
            Console.WriteLine(string.Join(Environment.NewLine, pairs));
        }


    }
}

输出

Test Name:  ToDictionarySpec
Test Outcome:   Passed
Result StandardOutput:  
1 : 1,2,3
2 : 1
3 : 1,2
32 : 2
4 : 2,3
52 : 3
43 : 3
于 2013-05-28T07:32:39.170 回答