Lal*_*ali 18 c# anonymous-types
我有一个匿名类型的列表
var myList = db.Products.Select(a => new {a.ProductName, a.ProductId,
a.Priority}).ToList();
Run Code Online (Sandbox Code Playgroud)
我想在此列表中添加其他项目,例如
myList.Insert(0, new { "--All--", 0, 0}); //Error: Has some invalid arguments
Run Code Online (Sandbox Code Playgroud)
我也试过了
myList.Add(new { "--All--", 0, 0}); //Error: Has some invalid arguments
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
编辑:
我在第一次回答后这样做了
var packageList = db.Products.Select(a => new {
a.ProductName, a.ProductId, a.Priority }).ToList();
packageList.Insert(0, new { ProductName = "All", ProductId = 0, Priority = 0 });
Run Code Online (Sandbox Code Playgroud)
但同样的错误.
Ser*_*kiy 31
您应该指定您创建的匿名对象的属性名称:
myList.Insert(0, new { ProductName = "--All--", ProductId = 0, Priority = 0});
Run Code Online (Sandbox Code Playgroud)
请记住 - 您应该列出匿名类型的所有属性(名称应该相同),它们应该以相同的顺序使用,并且它们应该具有完全相同的类型.否则将创建不同匿名类型的对象.
| 归档时间: |
|
| 查看次数: |
9257 次 |
| 最近记录: |