我试图在LINQ查询中使用反射来将十进制空值更改为字符串值。我的代码产生这个错误;如何将空值转换为LINQ到实体的字符串
“System.String类型的对象不能转换为类型System.Nullable System.Decimal”
感谢您的帮助。
public class ReportData
{
public IEnumerable<SASF> GetLongReportData(string commSubGp)
{
var context = new Entities();
string myDate = "2014-03-18";
DateTime date = Convert.ToDateTime(myDate);
var result = new List<SASF>();
if (commSubGp == "F00")
{
result = (from a in context.SASF
where a.RDate == date &&
a.COMM_SGP.CompareTo("F00") <= 0
orderby a.Conmkt, a.MKTTITL descending
select a).ToList();
//Here I'm trying to use reflection to loop through the object and set any value that's null to string value
result.ForEach(reflect =>
{
reflect.GetType().GetProperties().ToList().ForEach(p =>
{
var checkValue = p.GetValue(reflect, null);
if (checkValue == null)
{
p.SetValue(reflect, "non-reportable", null);
}
});
});
return result.ToList();
}
return results;
}
}
你提到你试图将一个小数改成一个字符串值,但是错误信息指出你试图将一个字符串转换为小数。 – Xpanse
SQL数据库表具有除ID列和日期时间列以外的所有十进制数据类型。 – NetBlazer
如果该类型已经是小数,则不能在该属性中存储字符串。您需要创建一个新的对象,该对象具有字符串属性并将数据投影到新对象中。 – Xpanse