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

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

问题描述

首先,我知道这是一个常见问题.但是我无法使用其他解决方案,因为我正在比较不能被替换为可空类型的类型,例如我的三元运算符中的DataRow类型.我也无法格式化网格中的货币,因为我正在旋转数据并且必须将自动生成列设置为true.我也无法在其他地方设置货币格式,因为我希望一个调用来获取原始数据,并在应有的位置进行格式化.我需要使用LinqSql,因为这是我在将数据绑定到Telerik RadGrid之前准备数据的方式

First off, I know this is a commonly asked question. But I cannot use other solutions because I am comparing types that cannot be delcared nullable types such as a DataRow type in my ternary operator. I also cannot format currency in my grid because I am pivoting my data and must have autogenerate columns set to true. I also cannot format currency somewhere else because I want one call to get the raw data and formatting to occur where is should. I need to use LinqSql because that is how I am preparing the data before binding it to a Telerik RadGrid

var drResults = from t1 in DatesTable.AsEnumerable()
                join t2 in dtResult.AsEnumerable() on t1["MONTH_YEAR"] equals t2["MONTH_YEAR"] into t1_t2
                from t2 in t1_t2.DefaultIfEmpty()
                select new
                {
                    MONTH_YEAR = t1.Field<string>("MONTH_YEAR"),
                    Appraisal_Fees = t2 != null ? String.Format("{0:C}", t2.Field<decimal>("AppraisalFees")) : 0): 0
                };

推荐答案

尝试将argement内部的三元表达式移动到String.Format.

Try moving the ternary expression inside the argement to String.Format.

String.Format("{0:C}", (t2 != null ? t2.Field<decimal>("AppraisalFees") : 0M)) 

您还可以通过将其写在两行中来使其更易于阅读:

You could also make it easier to read by writing it on two lines:

decimal appraisalFees = (t2 != null ? t2.Field<decimal>("AppraisalFees") : 0M);
Appraisal_Fees = String.Format("{0:C}", appraisalFees);

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

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