/// <summary> /// Maps data table to object list. /// </summary> /// <typeparam name = "T">The destination object type. Must have a default constructor.</typeparam> /// <param name = "source">The source data table.</param> /// <param name = "dataTypeTriggers">The generic data triggers (null for no trigger).</param> /// <param name = "aliases">Property name aliases ([destination name, source name]) Can be null.</param> /// <param name = "exclusions">Source object properties that should not be mapped. Can be null.</param> /// <param name="ignoreMissingField"></param> /// <returns></returns> public static IEnumerable <T> MapToObjectList <T>(this DataTable source, DataTypeTriggers dataTypeTriggers, Aliases aliases, Exclusions exclusions, bool ignoreMissingField) where T : new() { var list = new List <T>(); var properties = GenericTypeHelper.GetProperties <T>(); foreach (DataRow row in source.Rows) { var destination = new T(); foreach (var targetProperty in properties) { var targetValue = GetColumnValue(aliases, targetProperty, exclusions, row, ignoreMissingField); if (!(targetValue is NoValue)) { var type = source.Columns[GetColumnName(targetProperty.Name, aliases)].DataType; if (dataTypeTriggers != null && dataTypeTriggers.Keys.Contains(type)) { if (targetValue != DBNull.Value) { targetValue = dataTypeTriggers[type](targetValue); } } SetValue(aliases, targetProperty, targetValue, destination); } } list.Add(destination); } return(list); }
/// <summary> /// Maps data table to object list. /// </summary> /// <typeparam name = "T">The destination object type. Must have a default constructor.</typeparam> /// <param name = "source">The source data table.</param> /// <param name = "propertyNameTriggers">The column name triggers. Key is based on the column name.</param> /// <param name = "aliases">Property name aliases ([destination name, source name]) Can be null.</param> /// <param name = "exclusions">Source object properties that should not be mapped. Can be null.</param> /// <param name="ignoreMissingField">Field yoksa ignore et</param> /// <returns></returns> public static IEnumerable <T> MapToObjectList <T>(this DataTable source, PropertyNameTriggers propertyNameTriggers, Aliases aliases, Exclusions exclusions, bool ignoreMissingField) where T : new() { //if (useparallel) //{ // var results = new List<T>(); // var chunkedList = ConvertionExtensions.BreakIntoChunks<DataRow>(source.AsEnumerable().ToList(), 5000);//biner biner parcala // Parallel.ForEach(chunkedList, t => // { // var result = Map<T>(t); // lock (results) // { // results.AddRange(result); // } // }); // return results; //} var list = new List <T>(); var properties = GenericTypeHelper.GetProperties <T>(); foreach (DataRow row in source.Rows) { var destination = new T(); foreach (var targetProperty in properties) { var targetValue = GetColumnValue(aliases, targetProperty, exclusions, row, ignoreMissingField); if (!(targetValue is NoValue)) { var columnName = GetColumnName(targetProperty.Name, aliases); if (propertyNameTriggers != null && propertyNameTriggers.Keys.Contains(columnName)) { if (targetValue != DBNull.Value) { targetValue = propertyNameTriggers[columnName](targetValue); } } SetValue(aliases, targetProperty, targetValue, destination); } } list.Add(destination); } return(list); }
public static IList <TO> MapToAnOtherObjectAsList <TFrom, TO>(this IEnumerable <TFrom> from) where TO : new() { var list = new List <TO>(); var properties = GenericTypeHelper.GetProperties <TO>().ToArray(); foreach (var itemFrom in from) { var destination = new TO(); var propertiesTarget = GenericTypeHelper.GetProperties <TFrom>(); foreach (var propertyInfo in propertiesTarget) { var prop = properties.FirstOrDefault(p => p.Name.ToLower().Equals(propertyInfo.Name.ToLower()) && p.PropertyType == propertyInfo.PropertyType); if (prop != null) { prop.SetValue(destination, propertyInfo.GetValue(itemFrom)); } } list.Add(destination); } return(list); }