Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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
C# CreateDocumentQuery未绑定到CLR对象_C#_.net_Azure_Deserialization_Azure Cosmosdb - Fatal编程技术网

C# CreateDocumentQuery未绑定到CLR对象

C# CreateDocumentQuery未绑定到CLR对象,c#,.net,azure,deserialization,azure-cosmosdb,C#,.net,Azure,Deserialization,Azure Cosmosdb,我有一个解决方案,问题是为什么会发生这种情况,如果这是一个好的解决方案。 背景: 在Azure DocumentDb中,我用一个集合将一个托管哈希分区到一个db中。 CLR对象从文档类继承。我已经在所有属性上添加了JsonProperty属性 不起作用的查询如下: var a = _client.CreateDocumentQuery<T>(_database.SelfLink) .Where(d => d.Id == id)

我有一个解决方案,问题是为什么会发生这种情况,如果这是一个好的解决方案。 背景: 在Azure DocumentDb中,我用一个集合将一个托管哈希分区到一个db中。 CLR对象从文档类继承。我已经在所有属性上添加了JsonProperty属性

不起作用的查询如下:

var a = _client.CreateDocumentQuery<T>(_database.SelfLink)
                .Where(d => d.Id == id)
                .AsEnumerable()
                .FirstOrDefault();
var a=\u client.CreateDocumentQuery(\u database.SelfLink)
.其中(d=>d.Id==Id)
.可计算的()
.FirstOrDefault();
它总是返回null(文档就在那里,我在门户中看到它)。我现在浪费了这么多时间,我能做的就是:

var a = _client.CreateDocumentQuery<Document>(_database.SelfLink, "SELECT * FROM c")
                    .AsEnumerable().Where(t => t.Id == id)
                    .FirstOrDefault();
var obj = JsonConvert.SerializeObject(b);
T parsed = JsonConvert.DeserializeObject<T>(obj);
var a=\u client.CreateDocumentQuery(\u database.SelfLink,“从c中选择*)
.AsEnumerable()。其中(t=>t.Id==Id)
.FirstOrDefault();
var obj=JsonConvert.SerializeObject(b);
T parsed=JsonConvert.DeserializeObject(obj);
这有多糟糕,不是吗? 有人知道为什么框架没有对我进行反序列化,为什么它没有找到第一个示例中的任何内容吗

更新:

实际上,上面的“解决方案”并没有反序列化所有属性。。 我有一个属性
Dictionary
,它不会反序列化到CLR属性中。根据intellisense,对象是CLR类型的,但它有很多基类信息。但它似乎嵌套在永恒之中。我真的看不出类型是什么,但在6层以上(我认为这是资源基类),我发现了一个私有的属性包,所有属性都是JTokens/jproperty(我真的不知道如何分辨它们是哪一个)。 这里有json数据,我需要的数据在实际对象中,它只是没有绑定到属性。 我尝试在资源类上使用.SetProperty()方法,这确实有效。但是当从DocumentClient获取时,应该反序列化并绑定它,不是吗? 我做错了什么

2个月后: 我再次研究了这个问题,结果发现,在我的代码中,对于一个地方来说,上述可怕的解决方法仍然是唯一能够获取我的文档的方法。不过,原因可以在链条的前面找到。该方法的参数是
Expression
。 我给这个打电话的链条是

public T CreateIfNotExists<T>(Guid id) where T : IBaseDocument
{
    var id = ProduceDocId(typeof(T), id);
    var result = _repository.GetSingle<T>(r => r.Id == id);
    ...
}
public T CreateIfNotExists(Guid id),其中T:IBaseDocument
{
var id=ProduceDocId(类型(T),id);
var result=\u repository.GetSingle(r=>r.Id==Id);
...
}
然后

public T GetSingle<T>(Expression<Func<T, bool>> predicate) where T : IBaseDocument
    {
        ... // error handling ommitted
        T res = _client.CreateDocumentQuery<T>(_database.SelfLink)
            .Where(predicate)
            .AsEnumerable()
            .FirstOrDefault();
        return res;
    }
public T GetSingle(表达式谓词),其中T:IBaseDocument
{
…//处理ommitted的错误
T res=\u client.CreateDocumentQuery(\u database.SelfLink)
.Where(谓词)
.可计算的()
.FirstOrDefault();
返回res;
}
参数“predicate”将计算为匿名方法闭包,即在调试器中显示为“c_uDisplayClass2”()。 像这样:

{s=>(Convert(s).Id==value(FooNameSpace.BarClass+c_udisplayClass2`1[FooNameSpace.FooClass]).someId)}
documentdb无法正确计算此值,它将始终返回null。
如果我获取匿名方法闭包计算结果的实际id,并将其传入,则会得到文档。

您不需要从DocumentDB反序列化读取文档。 这样的函数应该可以为您完成此任务(此函数中的类名与您的不同):

公共列表GetCandidateById(int candidateId)
{
var client=newdocumentclient(新Uri(EndpointUrl),AuthorizationKey);
Database Database=client.CreateDatabaseQuery().Where(db=>db.Id==DatabaseName).AsEnumerable().FirstOrDefault();
DocumentCollection DocumentCollection=client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(db=>db.Id==DocumentCollectionName).AsEnumerable().FirstOrDefault();
返回client.CreateDocumentQuery(documentCollection.DocumentsLink).Where(m=>m.CandidateId==CandidateId).选择(m=>m).ToList();
}
我在这里发布了DocumentDB的一个示例:


解决了!非常非常简单的答案,这都是由于我的无知和缺乏正确的研究努力(因为我付出了努力)

要访问的文档上的属性setter不能访问,即private或protected不正常

就这样

2个月后:
事实上,不是这样。请参阅原始问题的最后一部分。

问题在于我有一个托管哈希分区数据库,这意味着我无法在集合链接或文档链接上搜索,因为文档是根据“PartitionKeySource”属性计算的哈希分布在集合上的,因此我在所有这些对象上都有该属性。因此,上述建议不能用于这种情况。非常感谢您的研究和辛勤工作。救了我的头不撞墙。谢谢
{s => (Convert(s).Id == value(FooNameSpace.BarClass+<>c__DisplayClass2`1[FooNameSpace.FooClass]).someId)}
public List<Candidate> GetCandidateById(int candidateId)
{
    var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
    Database database = client.CreateDatabaseQuery().Where(db => db.Id == DatabaseName).AsEnumerable().FirstOrDefault();
    DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(db => db.Id == DocumentCollectionName).AsEnumerable().FirstOrDefault();

    return client.CreateDocumentQuery<Candidate>(documentCollection.DocumentsLink).Where(m => m.CandidateId == candidateId).Select(m => m).ToList();
}