【问题标题】:Map MongoDB array to C# dictionary将 MongoDB 数组映射到 C# 字典
【发布时间】:2023-01-01 23:32:51
【问题描述】:

有什么方法可以将对象的 Mongo 数组映射到 C# 字典。 比如我有一个文档:

{
  "_id": ...,
  "customerId": ...,
  "items": [
    { 
        "productId": ...,
        "productTitle": ...,
        "unitPrice": ...,
        "quantity": ...,
        "discount": ...
    }
  ]
}

我想将“项目”映射到一个字典,其中键是“productId”,值是整个对象

做这个的最好方式是什么?

【问题讨论】:

    标签: c# mongodb mongodb-.net-driver


    【解决方案1】:

    如果你有一个对象列表,你可以使用ToDictionary()enumerable.todictionary()来做到这一点

    list<yourObject> _list = ...
    Dictionary<string, yourObject> _Dict= _list.ToDictionary(i => i.productId, i => i);
    

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2019-07-30
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多