【问题标题】:Add an anonymous object to an anonymous list将匿名对象添加到匿名列表
【发布时间】:2011-03-09 01:32:13
【问题描述】:

我有一个存储在 b 中的匿名类型列表(来自列表),我想向列表中添加一个新对象。当我尝试将新的匿名对象添加到此列表时,我收到此错误:

“System.Collections.Generic.List.Add(AnonymousType#1)”的最佳重载方法匹配有一些无效参数。 而且我也收到此错误:参数 1:无法从 'AnonymousType#2' 转换为 'AnonymousType#1'

 var b = user.Orders.Select(i => new { id = i.BillingId , text = i.Billing.ToString() }).ToList();
 b.Add(new { id = 0 , text = "New Billing Address" });

提前致谢

【问题讨论】:

    标签: c# list anonymous-types


    【解决方案1】:

    BillingId 是什么类型?您可能需要在数字文字上添加后缀,或者进行强制转换,或者如果它可以为空,则获取 Value..

    【讨论】:

    • 成功了。结果证明 BillingId 是可以为空的,我只需要调用 .Value 就可以了(尽管 BillingId 永远不会为空)。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多