通过linq进行CosmosDB selfjoin

时间:2018-06-29 15:20:26

标签: .net linq azure-cosmosdb

我有一个包含如下文档的文档集合

{
  id: "123123541234"
  items: [
    {Name = "Item 1", Amount = 12.12},
    {Name = "Item 2", Amount = 4.00},
  ]
}

我可以编写如下的sql自联接查询以返回我想要的内容:

select c.id, i.Name, i.Amount
from c
join i in c.items

正如您所看到的,我的ID为123123541234的文档将被复制,对于嵌套数组中的每个项目一次,因此输出如下:

[ 
  {id = "123123541234", Name = "Item 1", Amount = 12.12 }, 
  {id = "123123541234", Name = "Item 2", Amount = 4.00} 
] 

但是,我想使用linq编写此查询,以保持对象引用和类型定义的强度。我看不到如何通过linq进行这种“扁平化”操作,

TL; DR:如何通过linq与cosmosdb进行自我联接?

1 个答案:

答案 0 :(得分:1)

假设您的类型看起来像这样-

this.productService.getProducts().subscribe(data => {

       Console.log(data)  // giving undefined here  
     }
 });

这是您可以执行的操作-

public class Container
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "items")]
    public Item[] Items { get; set; }
}

public class Item
{
    public string Name  { get; set; }
    public double Amount { get; set; }
}

public class FlattenedContainer
{
    public string Id { get; set; }
    public string Name { get; set; }
    public double Amount { get; set; }
}