小编kaf*_*fka的帖子

为什么可空字符串没有 hasValue() 方法?

我有一个包含可为空字符串的类,我想检查它们是否保持为空或有人设置了它们。

与字符串类似,该类包含可为空的整数,我可以通过与 .HasValue() 方法进行相等比较来执行此检查 - 似乎字符串没有这个?

那么如何检查它是否从 null 变为 notNull 呢?

public class Test 
{
    public string? a
    public string? b
    public int? c
} 

var oldQ = new Test(c=123)
var newQ = new Test(c=546)

bool isStilValid = newQ.c.HasValue() == oldQ.c.HasValue() //(this is not possible?)&& newQ.b.HasValue() == oldQ.b.HasValue()
Run Code Online (Sandbox Code Playgroud)

为什么这是不可能的?

c# nullable

8
推荐指数
1
解决办法
1万
查看次数

调用 api 作为应用程序启动的一部分?

我目前面临的情况是,我需要在应用程序启动过程中调用控制器?控制器由应用程序本身托管。这有可能吗?只需要在每次应用程序启动时触发即可。

c# rest controller asp.net-core

8
推荐指数
1
解决办法
6744
查看次数

如何从 JsonDocument 中提取具有指定名称的所有 JsonProperty 对象的所有值?

我有一个自由格式JsonDocument,其中包含一个JsonPropertywith name internalName。我的问题是,我没有模型可以解析它(因为没有预定义的 JSON 模式),并且该属性可以出现在任何级别、父级、子级等。

如何获取JsonElementJSON 属性的所有值internalName

到目前为止我尝试过的是这样的

    var namesUsedInLayout = Layout.RootElement.EnumerateObject().Where(x => x.Name == "internalName").Select(x => x.Value.ToString());
Run Code Online (Sandbox Code Playgroud)

但这似乎只为我提供顶层,而不是子层或子层的子层。

c# regex json system.text.json

5
推荐指数
1
解决办法
5812
查看次数

以递归方式迭代 JsonDocument

我需要迭代 JsonDocument 并根据我遇到的情况执行某种检查JsonValueKind

我尝试以这种方式进行验证检查:

public bool Dec(JsonElement Element)
{
    var ElementEnumeratable = Element.EnumerateObject();

    foreach (var Elm in ElementEnumeratable )
    {
        string name = Elm.Name;
        switch (Elm.Value.ValueKind)
        {
            case JsonValueKind.Array:
                var jArray = Elm.Value;
                return Dec(jArray);
            case JsonValueKind.String:
                string jString = Elm.Value.GetString();
                break;
            case JsonValueKind.Number:
                int jNumber = Elm.Value.GetInt32();
                break;
        }
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是当ElmValueKind数组时 - 我无法将它传递给Dec看起来JsonElement有 valuekind 作为数组的数组,无法转换为EnumerateObject

在这里做什么?

c# system.text.json

5
推荐指数
1
解决办法
4869
查看次数

在自定义 JsonConverter 中,如何确定 Utf8JsonReader 数字标记是小数还是长整型?

我有这个jsonconverter,它需要将给定的属性值转换为小数或长整型,具体取决于该值 - 但我似乎无法确定属性值何时为小数或长整型,因为 tokentype 只能检测数字。 .我该如何解决这个问题?

public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
    IDictionary<string, object> output = new Dictionary<string, object>();

    while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
    {
        string propertyName = reader.GetString();
        reader.Read();
        object? propertyValue = null;

        switch (reader.TokenType)
        {
            case JsonTokenType.Number:
                propertyValue = reader.GetInt64();  // or could be a decimal for where I should reader.GetDecimal()
                break;
            case JsonTokenType.String:
                if (reader.TryGetDateTime(out DateTime value))
                {
                    propertyValue = value;
                }
                else
                {
                    propertyValue = reader.GetString();
                } …
Run Code Online (Sandbox Code Playgroud)

c# json jsonconverter system.text.json

3
推荐指数
1
解决办法
2164
查看次数

请求匹配多个端点,但为什么呢?

我有一个有多个路由的控制器。

我正在尝试调用一个声明为的端点

GET: api/lookupent/2020-03-17T13:28:37.627691
Run Code Online (Sandbox Code Playgroud)

但这会导致此错误

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches: 

Controllers.RecordController.Get (API)
Controllers.RecordController.GetRecordRegisteredAt (API)
Run Code Online (Sandbox Code Playgroud)

但我不确定我明白为什么这有意义,因为这段代码

// GET: api/{RecordName}/{id}
[HttpGet("{RecordName}/{id}", Name = "GetRecord")]
public ActionResult Get(string RecordName, long id)


// GET: api/{RecordName}/{timestamp}
[HttpGet("{RecordName}/{timestamp}", Name = "GetRecordRegisteredAt")]
public ActionResult GetRecordRegisteredAt(string RecordName, string timestamp)
Run Code Online (Sandbox Code Playgroud)

为什么输入与这些端点匹配?

c# rest

2
推荐指数
1
解决办法
9466
查看次数

使用给定模式在 Postgres 中生成列

我需要生成一个 SQL 查询,它可以生成遵循某种模式的列

模式是这样存储的Hello [Name] [lastname]

其中namelastname都是表中的列。

如何在 end、start 和 ind Between 属性处添加常量字符串?

以及如何区分表列和我添加的列应包含的字符串?

我的查询目前看起来像这样 - 然后模式

ALTER TABLE IF public.nameRegistration
DROP COLUMN IF EXISTS generated_colum 
ADD COLUMN generated_colum TEXT generated ALWAYS as (|Pattern|) stored; 
Run Code Online (Sandbox Code Playgroud)

模式只是Hello Name lastname

sql string postgresql alter-table generated-columns

2
推荐指数
1
解决办法
506
查看次数

kubectl exec 返回无例外的错误消息?

我目前正在尝试在我的 kubernetes pod 上执行一个简单的 bash 命令,但似乎遇到了一些没有意义的错误。

如果我执行到 docker 容器并运行命令普通

I have no name!@kafka-0:/tmp$ if [ $(comm -13 <(sort selectedTopics) <(sort topics.sh) | wc -l) -gt 0 ];  then  echo "hello"; fi
Run Code Online (Sandbox Code Playgroud)

我得到 Hello 作为输出。

但是如果从外部执行相同的

kubectl exec --namespace default kafka-0 -c kafka -- bash -c "if [ $(comm -13 </tmp/selectedTopics </tmp/topics.sh| wc -l) -gt 0 ];  then  echo topic does not exist && exit 1; fi"
Run Code Online (Sandbox Code Playgroud)

然后我收到一条错误消息,指出 /tmp/topics.sh: No such file or directory

事件虽然我能够做到这一点

kubectl exec --namespace $namespace kafka-0 -c …
Run Code Online (Sandbox Code Playgroud)

bash kubernetes kubectl

0
推荐指数
1
解决办法
610
查看次数