简体   繁体   中英

Naming a class property in C#

I have a data in mongodb collection

  "2021": [
    {
      "id": 0,
      "entity": "FA3T1",
      "value": 16,
      "remarks": "GOODRICH ACTUATION SYSTEMS SAS"
    }
  ]

Can anyone please suggest what would be the name of the class to be mentioned in c# for the property so as to deserialize the data from mongodb

I have tried the below option:

_2021

2021 is not a valid property name in C#; you can use a valid property name and mark the property with a BsonElement attribute to connect it with the corresponding BSON property, eg:

[BsonElement("2021")]
public IEnumerable<MyType> Data2021 { get; set; }

However, if there is a 2021 element in BSON, there might also be a 2020 and 2022 . In this case, you could have a look at dictionary serialization that allows to fill a dictionary like with keys for the years and values for the corresponding data. You can use the BsonDictionaryOptions attribute in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM