将匿名对象添加到匿名列表 [英] Add an anonymous object to an anonymous list
问题描述
我有一个存储在b中的匿名类型列表(来自列表),我想向列表中添加一个新对象.当我尝试将新的匿名对象添加到此列表时,出现以下错误:
I have a list of anonymous types stored in b (which comes from a list) and i want to add a new object to the list. When i try to add a new anonymous object to this list i get this error:
"System.Collections.Generic.List.Add(AnonymousType#1)"的最佳重载方法匹配具有一些无效的参数. 而且我也收到此错误:参数1:无法从'AnonymousType#2'转换为'AnonymousType#1'
The best overloaded method match for 'System.Collections.Generic.List.Add(AnonymousType#1)' has some invalid arguments. And i also get this error: Argument 1: cannot convert from 'AnonymousType#2' to '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" });
预先感谢
推荐答案
BillingId
是什么类型?您可能需要在数字文字上加一个后缀或强制转换,或者如果它可以为空,则获取Value
.
What type is BillingId
? You may need a suffix on the digit literal, or a cast, or get the Value
if it is nullable..
这篇关于将匿名对象添加到匿名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!