小编Ber*_*ius的帖子

在C#中加入/合并数组

我有两个或更多数组 - 一个有ID,一个或多个有字符串值.我想将这些合并到一个哈希表中,以便我可以按ID查找值.

以下函数可以完成这项工作,但更短更甜的版本(LINQ?)会很好:

Dictionary<int, string[]> MergeArrays( IEnumerable<int> idCollection,
                                       params IEnumerable<string>[] valueCollections )
{
    var dict = new Dictionary<int, string[]>();

    var idL = idCollection.Count();
    while ( idL-- > 0 )
    {
        dict[idCollection.ElementAt( idL )] = new string[valueCollections.Length];

        var vL = valueCollections.Length;
        while ( vL-- > 0 )
            dict[idCollection.ElementAt( idL )][vL] = valueCollections[vL].ElementAt( idL );
    }

    return dict;
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

.net c# linq arrays c#-3.0

5
推荐指数
1
解决办法
8347
查看次数

标签 统计

.net ×1

arrays ×1

c# ×1

c#-3.0 ×1

linq ×1