无法确定条件表达式的类型,因为int和string之间没有隐式转换 [英] Type of conditional expression cannot be determined because there is no implicit conversion between int and string

查看:126
本文介绍了无法确定条件表达式的类型,因为int和string之间没有隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,我有大胆的和平错误:



 var isIdSortable = Convert.ToBoolean(Request [bSortable_1 ]); 

var isNameSortable = Convert.ToBoolean(Request [bSortable_2]);
var isAddressSortable = Convert.ToBoolean(Request [bSortable_3]);
var isTownSortable = Convert.ToBoolean(Request [bSortable_4]);

var sortColumnIndex = Convert.ToInt32(Request [iSortCol_0]);
Func< Order,string> orderingFunction =(c => sortColumnIndex == 1&& isIdSortable?c.OrderID:
sortColumnIndex == 2&& isNameSortable?c.CustomerID:
sortColumnIndex == 3 && isAddressSortable?c.ShipAddress:
sortColumnIndex == 4&& isTownSortable?c.ShipCountry:
);







OrderID是INT,CustomerID地址和国家是字符串



我尝试了什么:



i刚刚在

解决方案

正如错误消息所示,您尝试返回的其中一个属性被定义为 int ,并且无法隐式转换为字符串。我怀疑 OrderID CustomerID ,或两者兼而有。



您需要添加 .ToString()调用 int 属性。

 c.OrderID.ToString()



移动条件访问可能更好 Func 的外部,以便您只评估一次:

 Func< Order,string> orderingFunction; 
if(sortColumnIndex == 1&& isIdSortable)
{
orderingFunction = c => c.OrderID.ToString();
}
else if(sortColumnIndex == 2&& isNameSortable)
{
orderingFunction = c => c.CustomerID.ToString();
}
else if(sortColumnIndex == 3&& isAddressSortable)
{
orderingFunction = c => c.ShipAddress;
}
else if(sortColumnIndex == 4&& isTownSortable)
{
orderingFunction = c => c.ShipCountry; //真的吗?是镇,还是国家?
}
其他
{
orderingFunction = c =>的String.Empty;
}


使用



 c.OrderID。 ToString()





而不是



 c.OrderID 


This is the code and i have error to the bold peace :

var isIdSortable = Convert.ToBoolean(Request["bSortable_1"]);

           var isNameSortable = Convert.ToBoolean(Request["bSortable_2"]);
           var isAddressSortable = Convert.ToBoolean(Request["bSortable_3"]);
           var isTownSortable = Convert.ToBoolean(Request["bSortable_4"]);

           var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
           Func<Order, string> orderingFunction = (c =>  sortColumnIndex == 1 && isIdSortable ? c.OrderID :
             sortColumnIndex == 2 && isNameSortable ? c.CustomerID :
                                                          sortColumnIndex == 3 && isAddressSortable ? c.ShipAddress :
                                                          sortColumnIndex == 4 && isTownSortable ? c.ShipCountry :
                                                          "");




OrderID is INT , CustomerID Adress and Country are string

What I have tried:

i just explained the problem above in the section

解决方案

As the error message says, one of the properties you're trying to return is defined as an int, and can't be implicitly converted to a string. I would suspect either OrderID, CustomerID, or both.

You'll need to add a .ToString() call to the int property.

c.OrderID.ToString()


It would probably also be better to move the conditional access outside of the Func, so that you only evaluate it once:

Func<Order, string> orderingFunction;
if (sortColumnIndex == 1 && isIdSortable)
{
    orderingFunction = c => c.OrderID.ToString();
}
else if (sortColumnIndex == 2 && isNameSortable)
{
    orderingFunction = c => c.CustomerID.ToString();
}
else if (sortColumnIndex == 3 && isAddressSortable)
{
    orderingFunction = c => c.ShipAddress;
}
else if (sortColumnIndex == 4 && isTownSortable)
{
    orderingFunction = c => c.ShipCountry; // Really? Is it the town, or the country?
}
else
{
    orderingFunction = c => string.Empty;
}


use

c.OrderID.ToString()



rather than

c.OrderID


这篇关于无法确定条件表达式的类型,因为int和string之间没有隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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