最简单的是:
var vresult= result.Select(u => new { ID = u.id, Guid = u.Guid.Trim(), addTime = u.addTime }); //这是LINQ,而SqlSuger里面不支持这样写
或:
var result = db.Queryable<t_user>().Where(u => u.CompanyId == comid && u.Isdel == false).OrderBy(u => u.addTime, OrderByType.Desc).ToPageList(page + 1, 3); //先全部查出来,然后再用 tuple List<Tuple<int, DateTime?>> list = new List<Tuple<int, DateTime?>>(); foreach (t_user u in result) { list.Add(new Tuple<int, DateTime?>(u.id,u.addTime)); }
或:
List<Dictionary<string, Object>> list = new List<Dictionary<string, object>>(); Dictionary<string, Object> dic = null; foreach (t_user u in result) { dic = new Dictionary<string, object>(); dic.Add("id", u.id); dic.Add("Guid", u.Guid.Trim()); dic.Add("addTime", u.addTime); list.Add(dic); }
context.Response.Write(jhelper.ObjToJson(new { status = 200, msg = "查看成功", items = list, nextpage = page + 1 }));
using Newtonsoft.Json; using System; using System.IO; namespace MyWeb { public class JsonHelper { /// <summary> /// json传输函数 /// </summary> /// <param name="obj"></param> /// <returns></returns> public string ObjToJson(Object obj) { StringWriter sw; JsonSerializer serializer = new JsonSerializer(); sw = new StringWriter(); serializer.Serialize(new JsonTextWriter(sw), obj); return sw.GetStringBuilder().ToString(); } } }