查询documentDB

时间:2017-03-27 10:10:17

标签: c# exception-handling async-await azure-cosmosdb

我有一个包含大量文档的DocumentDB集合。其中一些是无效的。我试图根据某些条件异步检索文档。

下面的代码是在发现无效文档时抛出异常,该文档满足条件但无法映射到TEntity(例如:某些字段为null):

IDocumentQuery<TEntity> queryable = client.CreateDocumentQuery<TEntity>(documentCollectionUri)
                                               .Where(c=>c.Name!="raju").AsDocumentQuery();

                List<TEntity> resultList = new List<TEntity>();
                while (queryable.HasMoreResults)
                {
                    foreach (TEntity t in await queryable.ExecuteNextAsync<TEntity>()) //throws exception while parsing to TEntity
                    {
                        resultList.Add(t);
                    }
                }

我可以将FeedOptions参数设置为1,然后将try catch放在foreach循环中,但这不是有效的方法(这么多次调用)。

有什么方法可以抑制异常并获取所有匹配的文档?

3 个答案:

答案 0 :(得分:0)

使用非通用的CreateDocumentQuery并自行将Documents转换为TEntity:

IDocumentQuery<TEntity> queryable = client.CreateDocumentQuery(documentCollectionUri)
    .Where(c=>c.Name!="raju")
    .Select(e => e as TEntity) // or whatever it takes to convert to TEntity without throwing
    .Where(e => e != null)
    .AsDocumentQuery();

答案 1 :(得分:0)

使用** protected void OnListViewItemClick(object sender, AdapterView.ItemClickEventArgs e) { Intent intent = new Intent(this.Activity, typeof(Activity2)); StartActivity(intent); }** 的{​​{1}}重载并使用CreateDocumentQuery <T>过滤掉无效的文档。这些方面的东西:

SqlQuerySpec

答案 2 :(得分:0)

SDK使用Json.Net进行内部反序列化。您是否尝试使用NullValueHandling JsonProperty包装不可空的属性?

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let mediaType = info[UIImagePickerControllerMediaType] as! NSString
        self.imagePickerController.dismissViewControllerAnimated(true) {
            // 3
            if mediaType == kUTTypeMovie {

                guard let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path else { return }

                if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path) {
                    UISaveVideoAtPathToSavedPhotosAlbum(path,    self, #selector(GalleryView.video(_:didFinishSavingWithError:contextInfo:)), nil)

                }
            }else{
                //Image
                let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
                let imageData:NSData = UIImageJPEGRepresentation(selectedImage,0.5)!
                var strBase64:String = imageData.base64EncodedStringWithOptions(.EncodingEndLineWithLineFeed)
                strBase64 = strBase64.stringByReplacingOccurrencesOfString("+", withString: "%2B")
                self.uploadGalleryDataInServer("1", ext: "jpeg", content: strBase64)
            }
        }
    }

    func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject) {
        if let _ = error {
        }else{
           print("videoPath = \(videoPath)")
            let data = NSData(contentsOfFile: videoPath as String)

            var strBase64:String = NSData(contentsOfFile: videoPath as String)!.base64EncodedStringWithOptions(.EncodingEndLineWithLineFeed)
            strBase64 = strBase64.stringByReplacingOccurrencesOfString("+", withString: "%2B")


            self.uploadGalleryDataInServer("2", ext: ".mp4", content: strBase64)
        }
    }