0

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
                };
david.s
  • 11,283
  • 6
  • 50
  • 82
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • 3
    What line is the error at? If it is the last line of code (excluding brackets) then it is exactly the problem it says: your two alternatives are `String.Format`, which is of type `string` or `0` which is of type `int`. Change `...("AppraisalFees")) : 0): 0` to `...("AppraisalFees")) : 0): "0"` and you will have a ternary with two strings and no error. – IBBoard May 19 '12 at 19:50
  • This solution worked, I swear I thought I put quotes around the 0 and got a compile error, thanks for your help!! – Brian Ogden May 29 '12 at 04:19

1 Answers1

1

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);
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452