【问题标题】:Merge two arrays in a Key => Value在 Key => Value 中合并两个数组
【发布时间】:2014-07-30 18:37:51
【问题描述】:

我有两个长度相同的数组,我需要将第一个用作Key,将第二个用作Value

我的Key 是唯一的string,而我的Value 是双精度。 我有 6 只水豚。他们的名字是关键,他们的重量是磅。

我当前的代码:

        ViewBag.MeuBalaioDeCapivaras = new string[]
        {
            "Jerimúndia",
            "Genoveva",
            "Facibulana",
            "Carnavala",
            "Dentinhos Dourados",
            "Creusa"
        };

        ViewBag.PesoDeCadaCapivaraDoMeuBalaioDeCapivaras = new double[]
        {
            500.0,
            400.0,
            250.0,
            12.0,
            1589.0,
            87.3
        };

这是我的数组,我是 KeyValuePair:

var keyValuePair = new KeyValuePair<string, double>(ViewBag.NomeDaCapivara, ViewBag.PesoDeCadaCapivaraDoMeuBalaioDeCapivaras);

它编译完美,但最后它在运行时给了我一个异常:

=> An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code. Additional information: The best correspondence of the overloaded method.

我该怎么做?

【问题讨论】:

标签: c# arrays linq


【解决方案1】:

LINQ 有一个鲜为人知的功能可以做到这一点 - Zip。该函数接受两个集合并将它们合并为一个集合:

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

var numbersAndWords = numbers.Zip(words, (number, word) => new KeyValuePair<string,int>(word, number);

【讨论】:

    【解决方案2】:

    您的代码可以编译,因为ViewBagdynamic,这意味着没有编译时检查。

    如果你更换了

    var keyValuePair = new KeyValuePair<string, double>(ViewBag.NomeDaCapivara, ViewBag.PesoDeCadaCapivaraDoMeuBalaioDeCapivaras);
    

    如果你直接把数组放进去,你会得到你所期望的编译时错误:例如

    var keyValuePair = new KeyValuePair<string, double>(
        new string[] { "Jerimúndia", "Genoveva", "Facibulana", "Carnavala", "Dentinhos Dourados", "Creusa" },
    new double[] { 500.0, 400.0, 250.0, 12.0, 1589.0, 87.3 });
    

    给予:

    Compilation error (line x1, col y1): The best overloaded method match for 'System.Collections.Generic.KeyValuePair<string,double>.KeyValuePair(string, double)' has some invalid arguments
    Compilation error (line x2, col y2): Argument 1: cannot convert from 'string[]' to 'string'
    Compilation error (line x3, col y3): Argument 2: cannot convert from 'double[]' to 'double'
    

    老派的、明确的做法是这样的:

        var meuBalaioDeCapivaras = new string[]
        {
        "Jerimúndia", "Genoveva", "Facibulana", "Carnavala", "Dentinhos Dourados", "Creusa"
        };
    
        var pesoDeCadaCapivaraDoMeuBalaioDeCapivaras = new double[]
        {
        500.0, 400.0, 250.0, 12.0, 1589.0, 87.3
        };
    
        var list = new List<KeyValuePair<string, double>>();
    
        for (var i = 0; i < meuBalaioDeCapivaras.Length; i++)
        {
            list.Add(new KeyValuePair<string, double>(meuBalaioDeCapivaras[i], pesoDeCadaCapivaraDoMeuBalaioDeCapivaras[i]));
        }
    

    正如Felipe OrianiAvner Shahar-Kashtan 所说,如果您使用.NET 3.5 或更高版本,则可以使用Linq

    【讨论】:

    • 如果不检查长度是否会导致问题,因为 KeyValuePair 的预期类型不可为空,或者不可以?
    • @BrettWeber 是的,它会的。您还应该检查来自ViewBag 的类型。如果你打电话给.Remove(),结果发现它是一个存储库,而不是一个列表?
    • hrm.. 好的,这有帮助!我使用 linq 来尝试类似的行为,但如果这比我的实现更快,那么每当我有时间更新它时,我就能够避免这些陷阱。谢谢!
    猜你喜欢
    • 2022-01-25
    • 2021-04-14
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多