Json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式
JSON序列化
/// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { /// <summary> /// 对象序列化JSON /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static string JsonSerializer<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// JSON序列化对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonString"></param> /// <returns></returns> public static T JsonDeserialize<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T obj = (T)ser.ReadObject(ms); return obj; } }
xml序列化
/// <summary> /// XML反序列化 /// </summary> /// <param name="type">类型</param> /// <param name="xml">XML字符串</param> /// <returns></returns> public static object Deserialize(Type type, string xml) { try { using (StringReader sr = new StringReader(xml)) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(sr); } } catch (Exception e) { return null; } } /// <summary> /// 序列化 /// </summary> /// <param name="type">类型</param> /// <param name="obj">对象</param> /// <returns></returns> public static string Serializer(Type type, object obj) { MemoryStream Stream = new MemoryStream(); XmlSerializer xml = new XmlSerializer(type); try { //序列化对象 xml.Serialize(Stream, obj); } catch (InvalidOperationException) { throw; } Stream.Position = 0; StreamReader sr = new StreamReader(Stream); string str = sr.ReadToEnd(); sr.Dispose(); Stream.Dispose(); return str; }
图片转base64.base64转图片
/// <summary> /// 图片转base64 /// </summary> /// <param name="filename"></param> public static string ImgToBase64String(string filename) { Bitmap bmp = new Bitmap(filename); MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); String strbaser64 = Convert.ToBase64String(arr); return strbaser64; }
/// <summary> /// base64转图片 /// </summary> /// <param name="base64Code"></param> public static Bitmap Base64StringToImage(string base64Code) { byte[] arr = Convert.FromBase64String(base64Code); MemoryStream ms = new MemoryStream(arr); Bitmap bmp = new Bitmap(ms); ms.Close(); return bmp; }
生成缩略图
/// <summary> /// 生成缩略图 /// </summary> /// <param name="img">原始图片</param> /// <param name="thumbnailImagePath">缩略图地址</param> /// <param name="width">图片宽度</param> /// <param name="height">图片高度</param> /// <param name="p"></param> public static void CutImage(Image img, string thumbnailImagePath, int width, int height) { Image serverImage = img; //画板大小 int towidth = width; int toheight = height; //缩略图矩形框的像素点 int x = 0; int y = 0; int ow = serverImage.Width; int oh = serverImage.Height; if (ow > oh) { toheight = serverImage.Height * width / serverImage.Width; } else { towidth = serverImage.Width * height / serverImage.Height; } //新建一个bmp图片 Image bm = new Bitmap(width, height); //新建一个画板 Graphics g = Graphics.FromImage(bm); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.White); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(serverImage, new Rectangle((width - towidth) / 2, (height - toheight) / 2, towidth, toheight), 0, 0, ow, oh, GraphicsUnit.Pixel); //以jpg格式保存缩略图 bm.Save(thumbnailImagePath, ImageFormat.Jpeg); serverImage.Dispose(); bm.Dispose(); g.Dispose(); }
很多时候很多状态数据库里可能都是保存的int。
但是在web上要转换成string.由于model要映射到数据库。
一般不能随意添加字段。所以在此提供一个方便的数据转换方法。
至少不用写个循环去匹配每个字段
方式1
List<EntityBM_CompanyDept> result = data.Select(ConvertBM_CompanyDept).ToList(); /// <summary> /// 数据转换 /// </summary> /// <param name="model"></param> /// <returns></returns> [NonAction] protected virtual EntityBM_CompanyDept ConvertBM_CompanyDept(BM_CompanyDept model) { return new EntityBM_CompanyDept() { Id = model.Id, DeptId = model.DeptId, DeptName = model.DeptName, ParentDeptId = model.ParentDeptId, LevelCode = model.LevelCode, IsCostCenter = model.IsCostCenter, IsProfitCenter = model.IsProfitCenter, CompanyDeptStatus = model.CompanyDeptStatus, Result = "0" }; }
方式二
List<EntityBM_CompanyDept> l = data.Select<BM_CompanyDept, EntityBM_CompanyDept>((dr) => { EntityBM_CompanyDept m = new EntityBM_CompanyDept() { Id = dr.Id //这里写转换字段 }; return m; }).ToList<EntityBM_CompanyDept>();
方式三
List<EntityBM_CompanyDept> l1 = data.Select((BM_CompanyDept) => { EntityBM_CompanyDept m = new EntityBM_CompanyDept() { Id = BM_CompanyDept.Id,//转换字段 }; return m; }).ToList<EntityBM_CompanyDept>();
方式四
List<ExtraterritorialDataDto> rows = new List<ExtraterritorialDataDto>(); if (rows != null) { rows = rows.Select(a => new ExtraterritorialDataDto { Id = a.Id,//主键列 messagesubject = a.messagesubject,//主题 senderaddress = a.senderaddress,//发送者 Month = a.datetime.ToString(),//年月日 recipientaddress = a.recipientaddress//接受者 }).ToList(); }
方式五
List<ExtraterritorialDataDto> rows = new List<ExtraterritorialDataDto>(); if (rows != null) { var rows2 = rows.Select(a => new { Id = a.Id,//主键列 messagesubject = a.messagesubject,//主题 senderaddress = a.senderaddress,//发送者 Month = a.datetime.ToString(),//年月日 recipientaddress = a.recipientaddress//接受者 }).ToList(); }
方式四和方式五的差距在于一个是显式类型。一个是匿名类型