【问题标题】:C# - Ternary operator between Dictionary and Object[]C# - Dictionary 和 Object[] 之间的三元运算符
【发布时间】:2020-12-22 18:01:02
【问题描述】:

如何在 C# 中实现 Dictionary 类型和对象数组之间的三元运算符?

我尝试了以下,但给了我错误:

System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<string, string>>" and "object[]

    public async Task NovoEmail(Escrita.Email email)
    {
        try
        {
            var response = await $"{_configuration["MicrosoftGraph:Url"]}/users/{email.Remetente}/sendMail"
                            .WithOAuthBearerToken(await _graph.Token())
                            .PostJsonAsync(new
                            {
                                message = new
                                {
                                    subject = email.Assunto,
                                    body = new { contentType = "HTML", content = email.Corpo },
                                    toRecipients = email.Destinatarios.Select(destinatario => new { emailAddress = new { address = destinatario } }),
                                    ccRecipients = email.Copias.Select(copia => new { emailAddress = new { address = copia } }),
                                    attachments = email.Anexos.Count > 0 ? email.Anexos.Select(anexo => new Dictionary<string, string> {
                                        { "@odata.type", "#microsoft.graph.fileAttachment" },
                                        { "name", anexo.FileName },
                                        { "contentType", anexo.ContentType },
                                        { "contentBytes", _base64.FormFileToBase64(anexo) }
                                     }) : new object[] { }
                                }
                            });

            response.EnsureSuccessStatusCode();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

【问题讨论】:

  • 如果你使用 new Dictionary&lt;string, string&gt;() 会怎样? (或null)
  • 代替这个new object[]使用new Dictionary&lt;string, string&gt;()
  • 如果有多个Anexos,我认为你的代码会抛出An item with the same key has already been added.的运行时异常
  • 你为什么还要测试&gt;0?如果没有项目,email.Anexos.Select(...) 将返回一个空枚举。如果您确实需要它,那么您需要匹配 Enumerable.Empty&lt;Dictionary&lt;string,string&gt;&gt;() 以匹配 Select。
  • @IanMercer 同意,这似乎不需要。如果有人坚持,我也会联系Enumerable.Empty&lt;IDictionary&lt;string,string&gt;&gt;()。

标签: c# arrays linq object


【解决方案1】:

零元素条件不需要三元运算符:没有它你会得到一个IEnumerable&lt;Dictionary&lt;string,string&gt;&gt;():

email.Anexos.Select(anexo => new Dictionary<string, string> {
                                    { "@odata.type", "#microsoft.graph.fileAttachment" },
                                    { "name", anexo.FileName },
                                    { "contentType", anexo.ContentType },
                                    { "contentBytes", _base64.FormFileToBase64(anexo) }
                                 })

如果您需要一个数组,只需调用 .ToArray() 即可。如果枚举中没有元素,则会得到一个零长度数组。

如果由于某些其他原因您确实需要三元运算符,那么您的 Select 表达式的空等效项将是:

Enumerable.Empty<Dictionary<string,string>>()

【讨论】:

    猜你喜欢
    • 2016-05-15
    • 2015-07-16
    • 1970-01-01
    • 2011-10-08
    • 2019-07-09
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多