Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 翻开字典<;Guid,IList<;字符串>&燃气轮机;进入字典<;字符串,IList<;Guid>&燃气轮机;和林克?_C#_Linq_Dictionary - Fatal编程技术网

C# 翻开字典<;Guid,IList<;字符串>&燃气轮机;进入字典<;字符串,IList<;Guid>&燃气轮机;和林克?

C# 翻开字典<;Guid,IList<;字符串>&燃气轮机;进入字典<;字符串,IList<;Guid>&燃气轮机;和林克?,c#,linq,dictionary,C#,Linq,Dictionary,我有一个字典,它显示了一个实体可以拥有的所有名称 我想将其转换为查看映射到所有实体的所有名称。 因此: 变成 [["a" => "FFF", "EEE"], ["b" => "FFF"], ["c" => "EEE"]] 我知道用foreaches很容易做到这一点,但我想知道是否有办法使用LINQ/ToDictionary?private static void Main(string[]args) private static void Main(string[] ar

我有一个
字典
,它显示了一个实体可以拥有的所有名称

我想将其转换为查看映射到所有实体的所有名称。 因此:

变成

[["a" => "FFF", "EEE"],
 ["b" => "FFF"],
 ["c" => "EEE"]]
我知道用foreaches很容易做到这一点,但我想知道是否有办法使用LINQ/ToDictionary?

private static void Main(string[]args)
private static void Main(string[] args)
{
    var source = new Dictionary<Guid, IList<string>>
    {
        { Guid.NewGuid(), new List<string> { "a", "b" } },
        { Guid.NewGuid(), new List<string> { "b", "c" } },
    };

    var result = source
        .SelectMany(x => x.Value, (x, y) => new { Key = y, Value = x.Key })
        .GroupBy(x => x.Key)
        .ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());

    foreach (var item in result)
    {
        Console.WriteLine($"Key: {item.Key}, Values: {string.Join(", ", item.Value)}");
    }
}
{ var source=新字典 { {Guid.NewGuid(),新列表{“a”,“b”}, {Guid.NewGuid(),新列表{“b”,“c”}, }; var结果=源 .SelectMany(x=>x.Value,(x,y)=>new{Key=y,Value=x.Key}) .GroupBy(x=>x.Key) .ToDictionary(x=>x.Key,x=>x.Select(y=>y.Value).ToList()); foreach(结果中的var项目) { WriteLine($”键:{item.Key},值:{string.Join(“,”,item.Value)}”); } }
var dic=new Dictionary()
{
{“FFF”,新列表(){“a”,“b”},
{“EEE”,新列表(){“a”,“c”}
};
var res=dic.SelectMany(x=>x.Value,(x,y)=>new{Key=y,Value=x.Key})
.ToLookup(x=>x.Key,x=>x.Value);
字典d=新字典(){
{1,新字符串[]{“a”,“b”},
{2,新字符串[]{“a”,“d”},
{3,新字符串[]{“b”,“c”},
{4,新字符串[]{“x”,“y”}};
d、 SelectMany(kvp=>kvp.Value.Select(element=>new{kvp.Key,element}))
.GroupBy(g=>g.element,g=>g.Key)
.ToDictionary(g=>g.Key,g=>g.ToList());
private static void Main(string[] args)
{
    var source = new Dictionary<Guid, IList<string>>
    {
        { Guid.NewGuid(), new List<string> { "a", "b" } },
        { Guid.NewGuid(), new List<string> { "b", "c" } },
    };

    var result = source
        .SelectMany(x => x.Value, (x, y) => new { Key = y, Value = x.Key })
        .GroupBy(x => x.Key)
        .ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());

    foreach (var item in result)
    {
        Console.WriteLine($"Key: {item.Key}, Values: {string.Join(", ", item.Value)}");
    }
}
var dic = new Dictionary<string, List<string>>()
{
    {"FFF", new List<string>(){"a", "b"}},
    {"EEE", new List<string>(){"a", "c"}}
};

var res = dic.SelectMany(x => x.Value, (x,y) => new{Key = y, Value = x.Key})
             .ToLookup(x => x.Key, x => x.Value);
Dictionary<int,IList<string>> d = new Dictionary<int ,IList<string>>(){
{1,new string[]{"a","b"}},
{2,new string[]{"a","d"}},
{3,new string[]{"b","c"}},
{4,new string[]{"x","y"}}};

d.SelectMany(kvp => kvp.Value.Select(element => new { kvp.Key, element}))
 .GroupBy(g => g.element, g => g.Key)
 .ToDictionary(g => g.Key, g => g.ToList());