带有linq的子列表中的cosmosdb where子句

时间:2018-03-13 08:58:09

标签: c# linq azure-cosmosdb

我的根数据有几个子集合,我需要对其中的2个进行where子句,示例:

   {
        "id": "000092224369_0030",
  ....
        "orderData": {
          ...
          "request_secondary": {
            "_type": "request_secondary",
            "secondary_requests": [{
"secondary_description":"do something" }]
          },
          "partnership": {
            "contacts": [
              {
                "role": "AG",
                "first_name": "LIEBENS Eric",
                "last_name": null,
                "_type": "contact",
                "email": "eric.liebens@gmail.com",
                "tel1": "0495-543905",
                "tel2": null,
                "vat": null
              },
              {
                "role": "ZO",
                "first_name": "Coralie",
                "last_name": "Demeyere",
                "_type": "contact",
                "email": "coralie.demeyere@ores.net",
                "tel1": "069/256533",
                "tel2": null,
                "vat": null
              },
              {
                "role": "ZR",
                "first_name": "GASBARRO Gianni",
                "last_name": null,
                "_type": "contact",
                "email": null,
                "tel1": "0495-385479-0",
                "tel2": null,
                "vat": "BE0474281005"
              }
            ],
    ...

在这里,我需要执行一个查询,将任何secondary_description等于文本的记录或带有该文本的名称的联系人带回来。 它应该转换为sql for this:

SELECT c.id from c
join x  in c.orderData.request_secondary
join y in c.orderData.partnership.contacts
where x.secondary_description ='sometin' or y.first_name= 'sometin'

我试过这个解决方案: How to query sub document (list type) in Cosmos Db

它适用于一个子集合,但我不知道如何让它与几个selectmany一起工作... 我有什么方法可以在linq中做到这一点? 谢谢!

1 个答案:

答案 0 :(得分:1)

根据您的描述,我认为您的SQL需要稍微调整一下。

SELECT c.id from c
join x  in c.orderData.request_secondary.secondary_requests
join y in c.orderData.partnership.contacts
where x.secondary_description ='something' or y.first_name= 'something'

然而,结果中会有重复的数据。所以,我还建议您采用我在帖子中回答的stored procedureHow to query sub document (list type) in Cosmos Db

function sample() {
    var collection = getContext().getCollection();
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT * FROM root r',
        function (err, feed, options) {
            if (err) throw err;
            if (!feed || !feed.length) getContext().getResponse().setBody('no docs found');
            else {
                var returnResult = [];
                for(var i = 0;i<feed.length;i++){
                    var isContinue = true;
                    var array1 = feed[i].orderData.request_secondary.secondary_requests;
                    var array2 = feed[i].orderData.partnership.contacts;
                    for(var j = 0;i<array1.length;j++){
                        if(array1[j].secondary_description == 'something'){
                            returnResult.push(feed[i]);
                            isContinue=false;
                            break;
                        }
                    }
                    if(isContinue){
                        for(var k = 0;i<array2.length;k++){
                            if(array2[j].first_name == 'something'){
                                returnResult.push(feed[i]);
                                break;
                            }
                        }
                    }    
                }
                getContext().getResponse().setBody(returnResult);
            }
        });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

更新答案:

您可以按照doc

从SQL构建LINQ
client.CreateDocumentQuery().SelectMany((x) => 
x.orderData.requestSecondary.secondaryRequests.Where(
    s=>s.secondaryDescription == "something" 
) ||
x.orderData.partnership.contacts.Where(
    c=>c.firstName == "something" 
)

但是,我认为您仍需要解析客户端上结果集的重复数据。

希望它对你有所帮助。