我正在尝试为条件中的短变量赋0,它会引发编译时错误
无法将类型'int'隐式转换为'short'。一个明确的 转换存在(您是否缺少演员表?)
请参阅下面的屏幕截图。 salesLineInput.Discount是[FunctionName("GetKeys")]
public static async System.Threading.Tasks.Task<HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log, ExecutionContext context)
{
//get the publishing profile information from function argument
var queryStrings=req.GetQueryParameterDictionary();
string publishingUserName = "";
string publishingPassword = "";
string hostKey = "";
queryStrings.TryGetValue("publishingUserName", out publishingUserName);
queryStrings.TryGetValue("publishingPassword", out publishingPassword);
//get the JWT token to call the KUDU Api
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{publishingUserName}:{publishingPassword}"));
var apiUrl = new Uri($"https://{Environment.GetEnvironmentVariable("WEBSITE_CONTENTSHARE")}.scm.azurewebsites.net/api");
string JWT;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Basic {base64Auth}");
var result = client.GetAsync($"{apiUrl}/functions/admin/token").Result;
JWT = result.Content.ReadAsStringAsync().Result.Trim('"'); //get JWT for call funtion key
}
//get the key from KUDU
var siteUrl = new Uri($"https://{Environment.GetEnvironmentVariable("WEBSITE_CONTENTSHARE")}.azurewebsites.net");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + JWT);
string jsonResult = client.GetAsync($"{siteUrl}/admin/host/keys").Result.Content.ReadAsStringAsync().Result;
dynamic resObject = JsonConvert.DeserializeObject(jsonResult);
hostKey = resObject.keys[0].value;
}
var template = @"{'$schema': 'https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#', 'contentVersion': '1.0.0.0', 'parameters': {}, 'variables': {}, 'resources': [],
'outputs': {
'HostKey':{
'value': {hostKey},
'type' : 'string'
}
}
}";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(template, System.Text.Encoding.UTF8, "application/json");
return response;
}
,而SalesLineEntity.Discount是short
:
这是我尝试的其他一些代码(与屏幕截图中的代码不同):
Nullable<double>
为什么 if(SalesLineEntity.Discount.HasValue)
salesLineInput.Discount = (short)(SalesLineEntity.Discount * 100);
else
salesLineInput.Discount = 0;
起作用,但是三进制不起作用?
答案 0 :(得分:1)
投射整个声明到short
salesLineItem.Discount = (short)(condition ? somecalc : 0);
或
salesLineItem.Discount = condition ? somecalc : (short)0;