文档db linq查询,SelectMany之后的SelectMany

时间:2016-08-29 10:17:06

标签: azure-cosmosdb

我使用linq在documentdb集合中进行查询,该集合包含两个单独数组的项目,如:

{
 name: "Family 1",
 cars: ["Toyota", "Honda"],
 pets: ["Cat", "Dog"]
}

让我们说我想找一个拥有丰田的家庭,我会使用这样的查询:

var families = Client.CreateDocumentQuery<T>(dLink)
    .SelectMany(f => f.Cars
                .Where(car=> car == "Toyota")
                .Select(car => f));

这很好用。检查生成的sql查询时,它也看起来像预期的那样:

SELECT VALUE root FROM root JOIN car IN root[\"Cars\"] WHERE (car= \"OU=Brugere\")

如果我想找到所有拥有丰田和狗的家庭,我希望能够使用看起来像这样的查询:

var families = Client.CreateDocumentQuery<T>(dLink)
    .SelectMany(f => f.Cars
                .Where(car=> car == "Toyota")
                .Select(car => f))
    .SelectMany(f => f.Pets
                .Where(pet => pet == "Dog")
                .Select(pet => f));

此操作因失败而显示错误消息:

 {"errors":[{"severity":"Error","location":{"start":109,"end":111},"code":"SC2001","message":"Identifier 'pet' could not be resolved."}]}

检查生成的sql查询时,它看起来不像预期的那样:

SELECT VALUE root FROM root JOIN car IN root[\"Cars\"] WHERE ((car= \"Toyota\") AND (pet = \"Dog\")) "}

这显然是为什么&#39; pet&#39;无法解决,它错过了root中的宠物加入[\&#34; Pets&#34;]

如何通过documentdb中的linq创建两个不同的连接?

1 个答案:

答案 0 :(得分:0)

鉴于来自toString()的SQL输出,看起来LINQ提供程序中存在错误。我们将努力解决问题。

与此同时,我建议将查询作为SQL查询发出:

SELECT *
FROM c
WHERE ARRAY_CONTAINS(c.cars, "Toyota")
      AND ARRAY_CONTAINS(c.pets, "Cat")