Add item to an anonymous list

后端 未结 1 1726
轮回少年
轮回少年 2021-02-07 01:24

I have a list of anonymous type

var myList = db.Products.Select(a => new {a.ProductName, a.ProductId, 
a.Priority}).ToList();

And I want to

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 02:20

    You should specify property names of anonymous object you create:

    myList.Insert(0, new { ProductName = "--All--", ProductId = 0, Priority = 0});
    

    Keep in mind - you should list all properties of anonymous type (names should be same), they should be used in same order, and they should have exactly same types. Otherwise object of different anonymous type will be created.

    0 讨论(0)
提交回复
热议问题