C# sqlite在使用cast(sum(a) as decimal) 時認作int型的問題處理

來源:互聯網
上載者:User

標籤:cas   type   欄位   list   code   列表   sqlite   使用   ++   

sqlite使用cast(sum(a) as decimal),如果a小數部分都是0,那麼填充到Table時,Table中欄位會被認作System.Int64類型。

C# 中DataTable轉實體類的時候就會報錯,"類型“System.Int64”的對象無法轉換為類型“System.Decimal”。

實體類轉換函式原代碼如下:

 1         /// <summary>   2         /// 填充對象列表:用DataTable填充實體類 3         /// </summary>   4         public List<T> FillModel(DataTable dt) 5         { 6             if (dt == null || dt.Rows.Count == 0) 7             { 8                 return null; 9             }10             try11             {12                 List<T> modelList = new List<T>();13                 foreach (DataRow dr in dt.Rows)14                 {15                     //T model = (T)Activator.CreateInstance(typeof(T));  16                     T model = new T();17                     for (int i = 0; i < dr.Table.Columns.Count; i++)18                     {19                         PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);20                         if (propertyInfo != null && dr[i] != DBNull.Value)21                         {22                              propertyInfo.SetValue(model, dr[i], null);23                         }24                     }25 26                     modelList.Add(model);27                 }28                 return modelList;29             }30             catch (Exception ex) { throw ex; }31             finally32             {33             }34         }

解決辦法,轉換的時候先做判斷,如果不是預設類型,則根據實際類型建立新的對象,間接賦值,修改好的代碼如下(29,30行):

 1         /// <summary>   2         /// 填充對象列表:用DataTable填充實體類 3         /// </summary>   4         public List<T> FillModel(DataTable dt) 5         { 6             if (dt == null || dt.Rows.Count == 0) 7             { 8                 return null; 9             }10             try11             {12                 List<T> modelList = new List<T>();13                 foreach (DataRow dr in dt.Rows)14                 {15                     //T model = (T)Activator.CreateInstance(typeof(T));  16                     T model = new T();17                     for (int i = 0; i < dr.Table.Columns.Count; i++)18                     {19                         PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);20                         if (propertyInfo != null && dr[i] != DBNull.Value)21                         {22                             if (propertyInfo.PropertyType.FullName == dr[i].GetType().FullName)23                             {24                                 propertyInfo.SetValue(model, dr[i], null);25                             }26                             else27                             {28                                 //如果Table中的資料類型和自訂Entity類中的資料類型不一樣,以ntity類中的為準29                                 object a = Activator.CreateInstance(Type.GetType(propertyInfo.PropertyType.FullName), dr[i]);30                                 propertyInfo.SetValue(model, a, null);31                             }32                         }33                     }34                     modelList.Add(model);35                 }36                 return modelList;37             }38             catch (Exception ex) { throw ex; }39             finally40             {41             }42         }

 

C# sqlite在使用cast(sum(a) as decimal) 時認作int型的問題處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.