2014-05-08 38 views
0

我试图在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; 

      } 

     } 
+0

你提到你试图将一个小数改成一个字符串值,但是错误信息指出你试图将一个字符串转换为小数。 – Xpanse

+0

SQL数据库表具有除ID列和日期时间列以外的所有十进制数据类型。 – NetBlazer

+0

如果该类型已经是小数,则不能在该属性中存储字符串。您需要创建一个新的对象,该对象具有字符串属性并将数据投影到新对象中。 – Xpanse

回答

0

由于您的属性类型是十进制?字符串“不可报告”不能转换为十进制,并且该值无法设置。但是,您可以将其设置为零:

p.SetValue(reflect, Decimal.Zero, null) 

或者任何十进制值。

p.SetValue(reflect, Decimal.MinValue, null) 
p.SetValue(reflect, Decimal.MaxValue, null) 

不知道什么数据将被用于最终我不知道这是否合适与否。

0

使用反射来做到这一点可能不是最好的解决方案,因为它是一个相当昂贵的过程。利用下面的方法可以让你具体化,并以你认为合适的方式输出数据(尽管你也可以在数据库方面做到这一点)。

不知道SASF类的结构我刚刚创建了一个伪类。

这当然要求你专门映射每个字段到你的字符串类。您可能可以使用像AutoMapper(https://github.com/AutoMapper/AutoMapper)这样的工具为您完成此操作。

public class ReportData 
{ 
    public IEnumerable<SASFStringified> 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(); 

      var stringifiedResult = new List<SASFStringified>(); 
      foreach (var sasf in result) 
      { 
       stringifiedResult.Add(new SASFStringified 
       { 
        ID = sasf.ID, 
        Field1 = sasf.Field1.HasValue ? sasf.Field1.Value.ToString() : "non-reportable", 
        Field2 = sasf.Field2.HasValue ? sasf.Field2.Value.ToString() : "non-reportable", 
        DateField = sasf.DateField.ToShortDateString() 
       }); 
      } 

      return stringifiedResult; 
     } 

     return results; 

    } 
} 

public class SASF 
{ 
    public int ID { get; set; } 
    public decimal? Field1 { get; set; } 
    public decimal? Field2 { get; set; } 
    public DateTime DateField { get; set; } 
} 

public class SASFStringified 
{ 
    public int ID { get; set; } 
    public string Field1 { get; set; } 
    public string Field2 { get; set; } 
    public string DateField { get; set; } 
} 
相关问题