0

In this scenario, I have a dtData which is a DataTable of string. This DataTable contains of just 1 column. In that column we have date which is in the form of string. I wanted to convert this column to date type and store it in the list.

Rajat Pandit
  • 63
  • 2
  • 9

2 Answers2

1

I think a loop would be simplest:

CultureInfo enUS = new CultureInfo("en-US");
foreach(var r in dt.Rows)
  if(DateTime.TryParseExact((string)r[0], "DATE FORMAT HERE", enUS, DateTimeStyles.None, out var d)
    list.Add(d);

Could also use LINQ, but it feels more messy

CultureInfo enUS = new CultureInfo("en-US");
dt.AsQueryable()
  .Select(r => DateTime.TryParseExact((string)r[0], "DATE FORMAT HERE", enUS, DateTimeStyles.None, out var d) ? d : DateTime.MinValue)
  .Where(d => d > DateTime.MinValue)
  .ToList();
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0
public static class DBHelper<T>
{
    public static void ConvertDataRowToModel(T modelObj, DataRow dataRow)
    {
        var properties = from prop in modelObj.GetType().GetProperties()
                         select prop;

        foreach (var property in properties)
        {
            dynamic propertyValue = null;

            foreach (DataColumn col in dataRow.Table.Columns)
            {
                if (property.Name.ToUpper().Equals(col.ColumnName.ToString().ToUpper()))
                {
                    propertyValue = dataRow[col.ColumnName] != DBNull.Value ? dataRow[col.ColumnName] : null;
                    break;
                }
            }


            if (propertyValue != null)
            {
                var propertyType = property.PropertyType.FullName;
                if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    propertyType = property.PropertyType.GetGenericArguments()[0].FullName;
                }
                switch (propertyType)
                {
                    case "System.Int32":
                        {
                            property.SetValue(modelObj, Convert.ToInt32(propertyValue), null);
                            break;
                        }
                    case "System.Nullable[System.Int32]":
                        {
                            property.SetValue(modelObj, Convert.ToInt32(propertyValue), null);
                            break;
                        }
                    case "System.Int64":
                        {
                            property.SetValue(modelObj, Convert.ToInt64(propertyValue), null);
                            break;
                        }
                    case "System.Boolean":
                        {
                            property.SetValue(modelObj, Convert.ToBoolean(Convert.ToInt16(propertyValue)), null);
                            break;
                        }
                    case "System.String":
                        {
                            var val = WebUtility.HtmlDecode(Convert.ToString(propertyValue));
                            property.SetValue(modelObj, val, null);
                            break;
                        }
                    case "System.DateTime":
                        {
                            property.SetValue(modelObj, Convert.ToDateTime(propertyValue), null);
                            break;
                        }
                    case "System.Decimal":
                        {
                            property.SetValue(modelObj, Math.Round(Convert.ToDecimal(propertyValue), 2), null);
                            break;
                        }
                    case "System.Double":
                        {
                            property.SetValue(modelObj, Math.Round(Convert.ToDouble(propertyValue), 2), null);
                            break;
                        }
                    case "System.Byte[]":
                        {
                            property.SetValue(modelObj, (Byte[])(propertyValue), null);
                            break;
                        }
                    default:
                        {
                            Type t = Enum.GetUnderlyingType(property.PropertyType);
                            switch (t.FullName)
                            {
                                case "System.Int16":
                                    {
                                        property.SetValue(modelObj, Convert.ToInt16(propertyValue), null);
                                        break;
                                    }
                                case "System.Int32":
                                    {
                                        property.SetValue(modelObj, Convert.ToInt32(propertyValue), null);
                                        break;
                                    }
                            }
                            break;
                        }
                }
            }
        }
    }

    public static List<T> ConvertDataTableToModelList(DataTable dataTable)
    {
        List<T> modelList = new List<T>();

        foreach (DataRow dataRow in dataTable.Rows)
            {   var ob = Activator.CreateInstance<T>();

                ConvertDataRowToModel(ob, dataRow);
                modelList.Add(ob);
            }            
        return modelList;
    }
}

Above class is having generic method to convert data table to list. calling mechanism: Lstsample lstsample==DBHelper.ConvertDataTableToModelList(datasetsample.Tables[0]);

Always_a_learner
  • 1,254
  • 1
  • 8
  • 16