Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure 使用Linq的Select查询DocumentDB以获取属性列表_Azure_Azure Cosmosdb_Nosql - Fatal编程技术网

Azure 使用Linq的Select查询DocumentDB以获取属性列表

Azure 使用Linq的Select查询DocumentDB以获取属性列表,azure,azure-cosmosdb,nosql,Azure,Azure Cosmosdb,Nosql,使用Azure的DocumentDb和.NET API,我有以下方法非常适合检索整个文档的列表: public async Task<IEnumerable<T>> GetItemsAsync<T>(Expression<Func<T, bool>> predicate) { IDocumentQuery<T> query = _Client.CreateDocument

使用Azure的DocumentDb和.NET API,我有以下方法非常适合检索整个文档的列表:

   public async Task<IEnumerable<T>> GetItemsAsync<T>(Expression<Func<T, bool>> predicate)
    {            
        IDocumentQuery<T> query = _Client.CreateDocumentQuery<T>(
            UriFactory.CreateDocumentCollectionUri(_DatabaseId, _Collection),
            new FeedOptions { MaxItemCount = -1 })
            .Where(predicate)
            .AsDocumentQuery();

        List<T> results = new List<T>();
        while (query.HasMoreResults)
        {
            var item = await query.ExecuteNextAsync<T>();
            results.AddRange(item);
        }

        return results;
    }
现在,我并不总是想返回整个文档,特别是考虑到DocumentDb RU定价模型,所以我认为我应该能够添加一个。选择如下所示的投影:

 public async Task<List<TResult>> GetItemsAsync<T, TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> select)
        {
            IDocumentQuery<TResult> query = _Client.CreateDocumentQuery<T>(
                UriFactory.CreateDocumentCollectionUri(_DatabaseId, _Collection),
                new FeedOptions { MaxItemCount = -1 })
                .Where(predicate)
                .Select(select)
                .AsDocumentQuery();

            List<TResult> results = new List<TResult>();
            while (query.HasMoreResults)
            {
                var item = await query.ExecuteNextAsync<TResult>();
                results.AddRange(item);
            }

            return results;
        }
用法:

var rez = await _docs.GetItemsAsync<ApolloAssetDoc, Guid?>(x => x.MyVal == 5, x => x.ID);
但是第二个方法总是返回0个结果。很明显我找错人了


如果查询中选择了多个属性,则返回动态对象列表的正确方法是什么?例如,从d.DocType=0的项目d中选择d.id、d.MyVal,或者从d.DocType=0的项目d中选择一个属性的简单列表,例如,从d.DocType=0的项目d中选择d.id?

如果没有,我可以重新处理该问题[JsonPropertyPropertyName=id]用于实体类中的id属性。如果未包括该属性,请尝试使用以下代码:

public class ApolloAssetDoc
    {
        [JsonProperty(PropertyName = "id")]
        public Guid ID { get; set; }
        public string MyVal { get; set; }

    }

注意:该字段区分大小写。

很有意思!我确实定义了该属性,但我在ICOSMSDocument文档接口中定义了它。我没有意识到JsonProperty映射属性不能应用于接口中!可能适用于任何属性?感谢您的帮助!