字典和对象之间的C#-三元运算符[]
如何在C#中的字典类型和对象数组之间实现三元运算符 我试着吼叫,但给了我错误:字典和对象之间的C#-三元运算符[],c#,arrays,linq,object,C#,Arrays,Linq,Object,如何在C#中的字典类型和对象数组之间实现三元运算符 我试着吼叫,但给了我错误: System.Collections.Generic.IEnumerable“和”对象[] public async Task NovoEmail(Escrita.Email email) { try { var response = await $"{_configuration["MicrosoftGraph:Url&qu
System.Collections.Generic.IEnumerable“和”对象[]
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;
}
}
公共异步任务电子邮件(Escrita.Email)
{
尝试
{
var response=wait$“{u配置[“MicrosoftGraph:Url”]}/users/{email.remeente}/sendMail”
.WithOAuthBeareToken(wait_graph.Token())
.PostJsonAsync(新版本)
{
消息=新
{
主题=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}),
附件=email.Anexos.Count>0?email.Anexos.Select(anexo=>newdictionary{
{“@odata.type”、“#microsoft.graph.fileAttachment”},
{“name”,anexo.FileName},
{“contentType”,anexo.contentType},
{“contentBytes”,_base64.FormFileToBase64(anexo)}
}):新对象[]{}
}
});
response.EnsureSuccessStatusCode();
}
捕获(例外情况除外)
{
掷骰子;
}
}
零元素条件不需要三元运算符:没有它,您将得到一个IEnumerable()
:
零元素条件不需要三元运算符:没有它,您将得到一个
IEnumerable()
:
如果您使用
新字典()
,该怎么办?(或null
)使用new Dictionary()
而不是这个new object[]
我认为您的代码会抛出运行时异常,即已经添加了一个具有相同键的项。
如果有多个Anexos
为什么还要测试>0
email.Anexos.Select(…)
如果没有项目,将返回空枚举。如果您确实需要它,那么它是可枚举的.Empty()
您需要匹配Select
@IanMercer同意,这似乎不需要。如果有人坚持,我也会选择Enumerable.Empty()尽管。如果你使用新字典()
?(或null
)使用new Dictionary()
而不是这个new object[]
我认为您的代码会抛出运行时异常,即已经添加了一个具有相同键的项。
如果有多个Anexos
为什么还要测试>0
email.Anexos.Select(…)
如果没有项目,将返回空枚举。如果您确实需要它,那么它是可枚举的.Empty()
您需要匹配Select
@IanMercer同意,这似乎不需要。如果有人坚持,我也会选择Enumerable.Empty()。
email.Anexos.Select(anexo => new Dictionary<string, string> {
{ "@odata.type", "#microsoft.graph.fileAttachment" },
{ "name", anexo.FileName },
{ "contentType", anexo.ContentType },
{ "contentBytes", _base64.FormFileToBase64(anexo) }
})
Enumerable.Empty<Dictionary<string,string>>()