-2

我有 2 个不同的系列。

伪代码:

// index string by int : Dictionary<int, string>
index = { 0, "a" }, { 1, "b" }, { 2, "c" }

// data : Dictionary<string, List<Data>>
data = {"a", { "data00", "data01"..}}, 
       {"b", {"data20", "data21", "data22"...}}, 
       {"c", {"data4",...}}...

我想要项目 int 索引到数据字符串值和

var result = data.SelectMany ... new { IntIndex, DataValue }

我需要将列表展平为一个序列,并使用 index 将Data值与int索引配对string

4

3 回答 3

1

我稍微更新了类型和值(您的字典包含重复的键并且未指定索引),但这应该不是问题。您可以轻松地为您的数据类型修改函数。

var index = new List<Tuple<int, string>> {Tuple.Create(0, "a"), Tuple.Create(1, "b")};

var data = new Dictionary<string, IEnumerable<string>>()
    {
        {"a", new[] {"data00", "data01"}},
        {"b", new[] {"data20", "data21", "data22"}},
        {"c", new[] {"data4"}}
    };

var result = index
    .Join(data, x => x.Item2, y => y.Key, (x,y) => new KeyValuePair<int, IEnumerable<string>>(x.Item1, y.Value))
    .SelectMany(x => x.Value, (x, y) => new KeyValuePair<int, string>(x.Key, y));
于 2013-06-16T19:00:51.827 回答
1

假设你的重复keys是偶然的,你可以试试这个

Dictionary<int, List<Data>> intData = new Dictionary<int, List<Data>>();

foreach (var iVal in index)
{
     List<Data> tmpList = new List<Data>();

     if (data.TryGetValue(iVal.Value, out tmpList))
     {
         intData.Add(iVal.Key, tmpList);
     }
}

如果您可以有重复的键,那么字典不是正确的结构。

于 2013-06-16T19:03:53.740 回答
0
var index = new List<Tuple<int, string>> {Tuple.Create(0, "a"), Tuple.Create(1, "b")};

var data = new Dictionary<string, IEnumerable<string>>()
{
    {"a", new[] {"data00", "data01"}},
    {"b", new[] {"data20", "data21", "data22"}},
    {"c", new[] {"data4"}}
};

var res = 
(from i in index
join d in data on i.Item2 equals d.Key
select new {Key = i.Item1, Value = d.Value})
.SelectMany(x => x.Value, (x, v) => new {x.Key, Value = v});
于 2017-11-11T18:01:39.573 回答