/// <summary> /// 将数据从查询结果容器中映射到实体中 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public IEnumerable <T> Map <T>() where T : EntityBase, new() { #region 旧方法代码,注释 /* * if (this.Values == null) * { * int rowsCount = this.Execute(); * if (rowsCount <= 0) * yield break; * * } * if (this.Values != null && this.fieldNames != null) * { * if (this.Values.Count == 0) * yield break; * * Dictionary<string, int> dictNameIndex = new Dictionary<string, int>(); * T entity = new T(); * string tabeName = entity.TableName; * //查找字段匹配情况 * //entity.PropertyNames 存储的仅仅是查询出来的列名称,由于有连表查询, * //如果要映射到指定的实体,还得检查当前列对应的表名称 * if (this.OQL.sql_fields.Contains("[" + tabeName + "]")) * { * //是连表查询 * for (int i = 0; i < this.fieldNames.Length; i++) * { * for (int j = 0; j < entity.PropertyNames.Length; j++) * { * string cmpString = "[" + tabeName + "].[" + entity.PropertyNames[j] + "]"; * if (this.OQL.sql_fields.Contains(cmpString)) * { * dictNameIndex[this.fieldNames[i]] = i; * break; * } * } * * * } * } * else * { * for (int i = 0; i < this.fieldNames.Length; i++) * { * for (int j = 0; j < entity.PropertyNames.Length; j++) * { * if (this.fieldNames[i] == entity.PropertyNames[j]) * { * dictNameIndex[this.fieldNames[i]] = i; * break; * } * } * } * } * * //没有匹配的,提前结束 * if (dictNameIndex.Count == 0) * yield break; * * int length = entity.PropertyValues.Length; * foreach (object[] itemValues in this.Values) * { * for (int m = 0; m < length; m++) * { * //将容器的值赋值给实体的值元素 * string key = entity.PropertyNames[m]; * if (dictNameIndex.ContainsKey(key)) * entity.PropertyValues[m] = itemValues[dictNameIndex[key]]; * } * yield return entity; * //创建一个新实例 * entity = new T (); * } * } * else * { * throw new Exception("EntityContainer 错误,执行查询没有返回任何行。"); * } */ #endregion if (this.Values == null) { //可能执行本方法之前,并没有调用过 OQL的Select方法指定要查询的实体类属性,需要模拟调用一次。 if (this.OQL.selectedFieldInfo.Count == 0) { int fieldCount = 0; foreach (EntityBase entity in this.OQL.GetAllUsedEntity()) { for (int i = 0; i < entity.PropertyNames.Length; i++) { object value = entity[i];//模拟调用 fieldCount++; } } this.OQL.Select(new object[fieldCount]); } int rowsCount = this.Execute(); if (rowsCount <= 0) { yield break; } } if (this.Values != null && this.fieldNames != null) { if (this.Values.Count == 0) { yield break; } TableNameField[] fieldInfo = new TableNameField[this.OQL.selectedFieldInfo.Count]; //查找字段匹配情况 //entity.PropertyNames 存储的仅仅是查询出来的列名称 for (int i = 0; i < this.OQL.selectedFieldInfo.Count; i++) { TableNameField tnf = this.OQL.selectedFieldInfo[i]; tnf.Index = tnf.Entity.GetPropertyFieldNameIndex(tnf.Field); fieldInfo[i] = tnf; } foreach (object[] itemValues in this.Values) { EntityBase itemEntity = null; Type entityType = typeof(T); for (int m = 0; m < fieldInfo.Length; m++) { //将容器的值赋值给实体的值元素 EntityBase entity = fieldInfo[m].Entity; if (entity.GetType() == entityType) { itemEntity = entity; int index = fieldInfo[m].Index; entity.PropertyValues[index] = itemValues[m]; } } if (itemEntity == null) { throw new Exception("EntityContainer 错误,查询没有包含当前指定的实体类类型,请检查OQL语句。"); } T resultEntity = (T)itemEntity.Clone(); yield return(resultEntity); } } else { throw new Exception("EntityContainer 错误,执行查询没有返回任何行。"); } }
/// <summary> /// 二进制序列化 /// </summary> /// <param name="entity">实体类实例</param> /// <returns>字节数组</returns> public static byte[] BinarySerialize(EntityBase entity) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); Type factEntityType = entity.GetType(); EntityFields ef = EntityFieldsCache.Item(factEntityType); byte b; //写入实体类标记 bw.Write(ENTITY_ITEM_FLAG); int length = entity.PropertyValues.Length - 1; for (int i = 0; i <= length; i++) { object obj = entity.PropertyValues[i]; //if (obj == System.DBNull.Value) obj = null;//DBNull.Value在Convert 的时候会失败 //为每个属性添加null标记 if (obj == System.DBNull.Value || obj == null) { b = 0;//NULL标记 } else { if (length == i) { b = byte.MaxValue; } else { b = 1; //如果标志位为 byte.MaxValue ,则表示已经写入所有的属性字段,可用于以后添加实体属性但又要成功读取原有的记录 } } bw.Write(b); //写入标记 if (b > 0) { Type propertyType = ef.GetPropertyType(entity.PropertyNames[i]); if (propertyType == null) { throw new Exception("PDF.NET实体类序列化错误:未知的实体属性类型,请检查实体类的属性和字段定义是否匹配。"); } switch (propertyType.Name) { case "Int32": bw.Write(Convert.ToInt32(obj)); break; case "String": bw.Write(Convert.ToString(obj)); break; case "DateTime": bw.Write(Convert.ToDateTime(obj).ToBinary()); break; case "Int16": bw.Write(Convert.ToInt16(obj)); break; case "Int64": bw.Write(Convert.ToInt64(obj)); break; case "Single": bw.Write(Convert.ToSingle(obj)); break; case "Double": bw.Write(Convert.ToDouble(obj)); break; case "Decimal": bw.Write(Convert.ToDecimal(obj)); break; case "Boolean": bw.Write(Convert.ToBoolean(obj)); break; case "Byte": bw.Write(Convert.ToByte(obj)); break; case "Char": bw.Write(Convert.ToChar(obj)); break; case "Byte[]": Byte[] buffer = (Byte[])obj; bw.Write(buffer.Length); //写入字节序列的长度 if (buffer.Length > 0) { bw.Write(buffer); } break; } } } bw.Close(); ms.Close(); return(ms.ToArray()); }