RavenDB中的对象列表

本文关键字:列表 对象 RavenDB | 更新日期: 2023-09-27 17:57:57

我正在尝试在MVC项目中学习RavenDB。。。假设我们有许多文档存储在RavenDB集合中,看起来像这样:

"ShipVia": "shippers/2",
  "Freight": 18.66,
  "Lines": [
    {
      "Product": "products/25",
      "ProductName": "NuNuCa Nuß-Nougat-Creme",
    },

通过这样做,我成功地显示了一个单独的属性(例如"运费"):类别:

public class Orders
    {
       public string Freight { get; set; } 
    }

然后使用列表模板创建一个硬类型视图。。。这给了我数据库中所有Freights的属性。。。

我想掌握"Lines"属性,并能够看到产品和产品名称。。。尝试过这个:

namespace World.DAL
{
    public class Orders
    {
        public string Company { get; set; }
        public string Employee { get; set; }
        public List<Lines> Lines { get; set; }
    }
    public class Lines
    {
       public string Product { get; set; }
       public string ProductName { get; set; }
    }
}

那没用。。我想刚刚返回了一个空列表。。有人能给我指正确的方向吗?

编辑:

 {
      "Company": "companies/65",
      "Employee": "employees/5",
      "OrderedAt": "1997-12-02T00:00:00.0000000",
      "RequireAt": "1997-12-30T00:00:00.0000000",
      "ShippedAt": "1997-12-08T00:00:00.0000000",
      "ShipTo": {
        "Line1": "2817 Milton Dr.",
        "Line2": null,
        "City": "Albuquerque",
        "Region": "NM",
        "PostalCode": "87110",
        "Country": "USA"
      },
      "ShipVia": "shippers/2",
      "Freight": 18.66,
      "Lines": [
        {
          "Product": "products/25",
          "ProductName": "NuNuCa Nuß-Nougat-Creme",
          "PricePerUnit": 14.0,
          "Quantity": 35,
          "Discount": 0.25
        },
        {
          "Product": "products/75",
          "ProductName": "Rhönbräu Klosterbier",
          "PricePerUnit": 7.75,
          "Quantity": 18,
          "Discount": 0.0
        }
      ]
    }

RavenDB中的对象列表

您的类需要与json格式完全匹配,这意味着您的类需要如下所示:

public class Orders
{
    public string ShipVia { get; set; }
    public string Freight { get; set; }
    public List<Lines> Lines { get; set; }
}

当然,您的线路类仍然需要声明为

public class Lines
{
   public string Product { get; set; }
   public string ProductName { get; set; }
}