/// <summary> /// Gets the property values. /// </summary> /// <param name="obj">The obj.</param> /// <param name="columnNames">The column names.</param> /// <returns>The values.</returns> public static object[] GetPropertyValues(object obj, params string[] columnNames) { Check.Require(obj != null, "obj could not be null."); Check.Require(obj is DynEntity, "obj must be an DynEntity."); Check.Require(columnNames != null, "columnNames could not be null."); Check.Require(columnNames.Length > 0, "columnNames's length could not be 0."); DynEntity entityObj = obj as DynEntity; string[] names = entityObj.GetPropertyMappingColumnNames(); object[] values = entityObj.GetPropertyValues(); List <object> lsValues = new List <object>(); foreach (string item in columnNames) { for (int i = 0; i < names.Length; i++) { if (names[i] == item) { lsValues.Add(values[i]); break; } } } return(lsValues.ToArray()); }
/// <summary> /// Convert an entity array to a data table. /// </summary> /// <param name="objs">The entity array.</param> /// <returns>The data table.</returns> public static DataTable EntityArrayToDataTable(DynEntityType entityType, DynEntity[] objs) { EntityConfiguration ec; if (objs != null && objs.Length > 0) { ec = MetaDataManager.GetEntityConfiguration(objs[0].EntityType.FullName); } else { ec = MetaDataManager.GetEntityConfiguration(entityType.FullName); } DataTable table = new DataTable(ec.Name); foreach (PropertyConfiguration pc in ec.Properties) { if ((!pc.IsQueryProperty) || (pc.QueryType == "FkReverseQuery")) { DataColumn column = new DataColumn(pc.QueryType == "FkReverseQuery" ? pc.MappingName : pc.Name, Util.GetType(pc.PropertyMappingColumnType.Replace("System.Nullable`1[", "").Replace("]", ""))); table.Columns.Add(column); } } if (objs != null && objs.Length > 0) { for (int i = 0; i < objs.Length; i++) { object[] values = DynEntity.GetPropertyValues(objs[i], DynEntity.GetPropertyMappingColumnNames(MetaDataManager.GetEntityConfiguration(objs[0].EntityType.Name))); DataRow row = table.NewRow(); int j = 0; foreach (PropertyConfiguration pc in ec.Properties) { if ((!pc.IsQueryProperty) || (pc.QueryType == "FkReverseQuery")) { object value = (values[j] == null ? DBNull.Value : values[j]); //if (pc.IsCompoundUnit) //{ // value = SerializationManager.Serialize(value); //} row[j] = value; } j++; } table.Rows.Add(row); } } table.AcceptChanges(); return(table); }