/// <summary> /// 将实体类的部分字段结果集或者实体类联和查询的字段的结果集映射到一个新的POCO类 /// </summary> /// <typeparam name="TResult">POCO类类型</typeparam> /// <param name="fun">包含实体属性访问的POCO类生成函数,注意至少包含2个以上的实体类属性访问</param> /// <returns></returns> public IList <TResult> MapToList <TResult>(ECMapFunc <TResult> fun) where TResult : class { if (executed) { throw new Exception("在执行 MapToList 方法之前,不能先执行 Execute 方法!"); } this.oql.fieldStack.Clear();//清除可能的调试信息 var result = fun(); int count = this.oql.fieldStack.Count; if (count < 1) { throw new ArgumentException("要将结果映射的查询的字段太少,请至少指定一个要查询的字段!"); } this.oql.Select(new object[count]); //已经获取到自定义实体类对象中选择的字段,可以用此OQL进行查询了,待完成 List <TResult> resultList = new List <TResult>(); foreach (var data in this.MapMoreEntity(this.OQL.GetAllUsedEntity())) { //执行之前,必须先给相应的实体对象赋值,否则报错 TResult obj = fun(); resultList.Add(obj); } return(resultList); }
/* * /// <summary> * /// 将结果映射到相应类型的列表(可以使匿名类型) * /// <example> * /// <code> * /// <![CDATA[ * /// OQL q=OQL.From(entity1) * /// .Join(entity2).On(entity1.PK,entity2.FK) * /// .Select(entity1.Field1,entity2.Field2) * /// .End; * /// EntityContainer ec=new EntityContainer(q); * /// var list=ec.MapToList(()=> * /// { * /// return new { * /// Property1=ec.GetItemValue<int>(0), * /// Property2=ec.GetItemValue<string>(1) * /// }; * /// }); * /// * /// foreache(var item in list) * /// { * /// Console.WriteLine("Property1={0},Property2={1}",item.Property1,item.Property2); * /// } * /// ]]> * /// </code> * /// </example> * /// </summary> * /// <typeparam name="TResult">要映射的结果类型</typeparam> * /// <param name="fun">自定义的返回结果类型的函数</param> * /// <returns>结果列表</returns> * public IList<TResult> MapToList<TResult>(MyFunc<TResult> fun) where TResult : class * { * if (this.Values == null) * this.Execute(); * List<TResult> resultList = new List<TResult>(); * if (this.Values != null && this.fieldNames != null) * { * foreach (object[] itemValues in this.Values) * { * * this.currValue = itemValues; * TResult t = fun(); * resultList.Add(t); * } * return resultList; * } * else * { * throw new Exception("EntityContainer 错误,执行查询没有返回任何行。"); * } * } */ #endregion /// <summary> /// 将实体类的部分字段结果集或者实体类联和查询的字段的结果集映射到一个新的POCO类 /// </summary> /// <typeparam name="T">OQL使用的实体类类型</typeparam> /// <typeparam name="TResult">要映射的结果类型</typeparam> /// <param name="para">OQL使用的实体类</param> /// <param name="fun">完成映射的用户方法</param> /// <returns>映射的用户类型数据列表</returns> public IList <TResult> MapToList <T, TResult>(T para, ECMapFunc <T, TResult> fun) where T : EntityBase, new() where TResult : class { this.oql.fieldStack.Clear(); var result = fun(para); int count = this.oql.fieldStack.Count; this.oql.Select(new object[count]); //已经获取到自定义实体类对象中选择的字段,可以用此OQL进行查询了 List <TResult> resultList = new List <TResult>(); foreach (T entity in this.Map <T>()) { TResult item = fun(entity); resultList.Add(item); } return(resultList); }
/// <summary> /// 将关联查询结果映射到POCO类型列表 /// </summary> /// <typeparam name="TResult">POCO类型</typeparam> /// <param name="q">实体类关联查询的OQL语句</param> /// <param name="db">数据访问对象</param> /// <param name="fun">映射方法委托</param> /// <returns>POCO类型列表</returns> public static IList <TResult> MapToList <TResult>(this OQL q, AdoHelper db, ECMapFunc <TResult> fun) where TResult : class { EntityContainer ec = new EntityContainer(q, db); return(ec.MapToList <TResult>(fun)); }