SQLite uses cast (sum (a) as decimal), and if the A fractional part is 0, the field in the table is considered the System.Int64 type when populated to the table.
When a DataTable goes to an entity class in C #, an error occurs andthe object "type System.Int64" cannot be converted to type "System.Decimal".
The original code for the entity class conversion function is as follows:
1 /// <summary>
2 /// Fill the object list: Fill the entity class with 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 try
11 {
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)
twenty one {
22 propertyInfo.SetValue (model, dr [i], null);
twenty three }
twenty four }
25
26 modelList.Add (model);
27}
28 return modelList;
29}
30 catch (Exception ex) {throw ex;}
31 finally
32 {
33}
34}
Solution, the conversion of the time to make a judgment, if not the default type, then create a new object according to the actual type, indirect assignment, the modified code is as follows (29,30 line):
1 /// <summary>
2 /// Fill the object list: Fill the entity class with 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 try
11 {
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)
twenty one {
22 if (propertyInfo.PropertyType.FullName == dr [i] .GetType (). FullName)
twenty three {
24 propertyInfo.SetValue (model, dr [i], null);
25}
26 else
27 {
28 // If the data type in the Table is not the same as the data type in the custom Entity class, the one in the NTt class shall prevail
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 finally
40 {
41}
42}
C # SQLite is treated as an int type when using cast (sum (a) as decimal)