类名不同,结构相同的两个类相互转换,忽略 实体null值的json转换;
/// <summary> /// 结构相同,类名不同的实体类转换(忽略 null /// kk 2015-06-03 /// </summary> /// <typeparam name="T">需转换的类 类型</typeparam> /// <typeparam name="V">目标类 类型</typeparam> /// <param name="a">需转换的类 对象</param> /// <returns></returns> private V ConvertClassBySameClassStructure<T, V>(T a) where T : class ,new() where V : class,new() { Newtonsoft.Json.JsonSerializerSettings jSetting = new Newtonsoft.Json.JsonSerializerSettings(); jSetting.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; string json = Newtonsoft.Json.JsonConvert.SerializeObject(a, jSetting); return SerializeUtil.DeserializeJson<V>(json); }
将集合若干组
/// <summary> /// 将集合若干组 /// </summary> /// <param name="source">数据集</param> /// <param name="pageSize">每一组大小</param> private List<List<T>> SpliteSourceBySize<T>(List<T> source, int pageSize) where T : class, new() { // 计算组数 List<List<T>> pages = new List<List<T>>(); //页码 0也就是第一条 int pageNum = 0; while (pageNum * pageSize < source.Count) { //分页 var query = source.Skip(pageNum * pageSize).Take(pageSize).ToList(); pages.Add(query); pageNum++; } return pages; }