The argument 'value' was the wrong type. Expected 'System.Nullable`1[System.Int32]'. Actual 'System.String'. RRS feed

  • Question

  • Hello,

    In my LINQ query, I have in the select statement:

    //Upper condition omitted
    select new MyObj
       ItemKey = (o.ForeignKeyEntries.Count > 0) ? o.ForeignKeyEntries[0].ItemKey : default(int?),
       ItemName = (o.ForeignKeyEntries.Count > 0) ? o.ForeignKeyEntries[0].ItemName : default(string),


    For some reason, when ItemKey condition runs, it gives me the error: "The argument 'value' was the wrong type. Expected 'System.Nullable`1[System.Int32]'. Actual 'System.String'.".  Why is that?  You can see that item key is an int.  I even tried to cast the true condition to (int?) and still the same error.  Where in this is a string???  It's an integer, but maybe LINQ is compiling it incorrectly???

    I'm really confused by this one.

    Wednesday, October 14, 2009 5:51 PM


All replies