【问题标题】:cosmosdb where clause in sub lists with linq带有 linq 的子列表中的 cosmosdb where 子句
【发布时间】:2018-07-26 15:02:48
【问题描述】:

我的根数据有几个子集合,我需要对其中的两个做一个 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 的东西:

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 中做到这一点? 谢谢!

【问题讨论】:

    标签: c# linq azure-cosmosdb


    【解决方案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" 
    )
    

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

    希望对你有帮助。

    【讨论】:

    • 感谢您的回答,虽然我明白您在说什么,有什么办法可以在 Linq 中实现我的查询吗? (仅用于一般知识)。抱歉耽搁了,疯狂的日子:)
    • 我觉得这种说法很奇怪,所以我试了一下。实际上它似乎不起作用,我这样做: var test = sut.CreateQuery().SelectMany((x) => x.Data.Attachments.Where( s => s.FileName == "something" ) | | x.Data.SecondaryRequest.SecondaryRequests.Where( c => c.SecondaryDescription == "something" ));我得到一个错误运算符 ||不能应用于 'Ienumable 和 'Ienumrable' 类型的操作数。 (没有采取我之前展示的确切对象,但它应该以相同的方式工作......我错过了什么?
    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2011-06-16
    • 2019-06-26
    • 1970-01-01
    相关资源
    最近更新 更多