如何在linq查询期间告诉DocumentDB SDK使用camelCase? [英] How to tell DocumentDB SDK to use camelCase during linq query?

查看:48
本文介绍了如何在linq查询期间告诉DocumentDB SDK使用camelCase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑存储在User集合中的文档{ "userName": "user1" }和以下User类:

Considering the document { "userName": "user1" } stored in the User collection, and the following User class:

public class User
{
        public string Id { get; set; }
        public string UserName { get; set; }
}

具有以下JSON.net设置:

With the following JSON.net settings:

JsonConvert.DefaultSettings = () =>
{
    return new JsonSerializerSettings
    {
       ContractResolver = new CamelCasePropertyNamesContractResolver(),
    };
};

当我这样查询Linq时:

When I query with Linq as such:

var t = _client.CreateDocumentQuery<User>(_collection.SelfLink)
            .Where(u => u.UserName == "user1").AsDocumentQuery().ExecuteNextAsync();

t.Wait();

var users = t.Result;
var user = users.FirstOrDefault();

user为空.更改文档以使用帕斯卡套或POCO以使用骆驼套可以解决此问题.当然,我不需要这些,因为我希望将JSON对象和C#对象标准化".

user is null. Changing the Document to have a pascal casing or the POCO to use a camel casing solves the issue. Of course I do not want any of those as I want my JSON objects and C# objects to be "standarized".

如何告诉DocumentDB SDK使用骆驼套(类似于JSON.net)映射对象的属性名称?

How can I tell the DocumentDB SDK to map my object's property names using camel casing, similar as JSON.net?

推荐答案

DocumentDB LINQ提供程序未获取JsonConvert.DefaultSettings.通常,您可以使用DefaultSettings来控制camelCase,但是对于要在LINQ Where子句中使用的属性,必须使用DTO上的JsonProperty属性明确设置名称.

The DocumentDB LINQ provider does not pick up the JsonConvert.DefaultSettings. In general you can use the DefaultSettings to control camelCase, but for those properties you wish to use in a LINQ Where clause must have the name explicitly set using JsonProperty attribute on your DTO.

public class User
{
    public string Id { get; set; }

    [JsonProperty("userName")]
    public string UserName { get; set; }
}

尽管有些乏味且是很好的bug来源,但它似乎是您目前唯一的选择.

Although a bit tedious and a good source for bugs, it seems to be your only option for now.

这篇关于如何在linq查询期间告诉DocumentDB SDK使用camelCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆