列出超过 8 个项目的元组

List Tuple more than 8 items(列出超过 8 个项目的元组)
本文介绍了列出超过 8 个项目的元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助以下列表元组超过 8 个元素不起作用:

Can anyone help the following List Tuple more than 8 elements is not working:

List<Tuple<int, string, double, string, int, string, double, Tuple<int, string>>> tpl = new 
List<Tuple<int, string, double, string, int, string, double, Tuple<int, string>>>();
tpl.Add(Tuple.Create(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123, new Tuple<int, string>(100, "My Rest Item")));

foreach(var k in tpl)
        listBox1.Items.Add(k.Item1.ToString() + " ---> " + k.Item2.ToString() + " ---> " + k.Item3.ToString() + " ---> " +
        k.Item4.ToString() + " ---> " + k.Item5.ToString() + " ---> " + k.Item6.ToString() + " ---> " +
        k.Item7.ToString() + " ---> " + k.Rest.Item1.ToString());

它给出以下错误

错误 1 ​​最好的重载方法匹配'System.Collections.Generic.List>>.Add(System.Tuple'有一些无效参数 C:UsersHewlettPackardAppDataLocalTemporary项目WindowsFormsApplication1Form1.cs 68 17 WindowsFormsApplication1和错误 2 参数 1:无法从'System.Tuple'到'System.Tuple' C:UsersHewlettPackardAppDataLocalTemporary项目WindowsFormsApplication1Form1.cs 68 25 WindowsFormsApplication1

Error 1 The best overloaded method match for 'System.Collections.Generic.List<System.Tuple<int,string,double,string,int,string,double,System.Tuple<int,string>>>.Add(System.Tuple<int,string,double,string,int,string,double,System.Tuple<int,string>>)' has some invalid arguments C:UsersHewlett PackardAppDataLocalTemporary ProjectsWindowsFormsApplication1Form1.cs 68 17 WindowsFormsApplication1 and Error 2 Argument 1: cannot convert from 'System.Tuple<int,string,double,string,int,string,double,System.Tuple<System.Tuple<int,string>>>' to 'System.Tuple<int,string,double,string,int,string,double,System.Tuple<int,string>>' C:UsersHewlett PackardAppDataLocalTemporary ProjectsWindowsFormsApplication1Form1.cs 68 25 WindowsFormsApplication1

推荐答案

问题在于 Tuple.Create.仔细看看参数返回值是如何定义的:

The problem is with the last argument to Tuple.Create. Look carefully at how the argument return value is defined:

public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>
    Create<T1, T2, T3, T4, T5, T6, T7, T8>(
    T1 item1,
    T2 item2,
    T3 item3,
    T4 item4,
    T5 item5,
    T6 item6,
    T7 item7,
    T8 item8
)

基本上,包装 T8 在一个Tuple 自动 - 有点无益.

Basically, that wraps T8 in a Tuple<T8> automatically - and somewhat unhelpfully.

您可以使用 new 代替:

var value = new Tuple<<int, string, double, string, int, string, double,
                      Tuple<int, string>>
    (1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123,
     new Tuple<int, string>(100, "My Rest Item"));

虽然这很可怕.自己创建一些静态方法可能会更好,例如

That's pretty ghastly though. It might be better to create some static methods yourself, e.g.

public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8, T9>>
    Create(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9)
{
    return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8, T9>>
        (t1, t2, t3, t4, t5, t6, t7, Tuple.Create(t8, t9)); 
}

(根据需要使用尽可能多的重载)

(with as many overloads as you need)

或者可能是 Tuple 上的扩展方法:

or possibly an extension method on Tuple<T1 ... T7>:

public static Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
    With(this Tuple<T1, T2, T3, T4, T5, T6, T7> tuple,
         TRest rest)
{
    return new Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(
        tuple.Item1, 
        tuple.Item2, 
        tuple.Item3, 
        tuple.Item4, 
        tuple.Item5, 
        tuple.Item6, 
        tuple.Item7,
        rest);
}

那么你可以使用:

var value = Tuple.Create(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123)
                 .With(Tuple.Create(100, "My Rest Item"));

我个人会尽量避免完全使用这种大小的元组 - 而是创建一个具有适当属性的命名类型.

Personally I'd try to avoid having this size of tuple entirely though - create a named type with the appropriate properties, instead.

这篇关于列出超过 8 个项目的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)