标签:des style blog http color os
string json1 = @"{ ""publicaccount"": { ""id"": ""1"", ""name"": ""abc"" }, ""root"": { ""messages"": [ { ""id"": ""1"", ""title"": ""messagetitle1"", ""content"": ""helloworld"", ""url"": ""http://web.abc"" }, { ""id"": ""2"", ""title"": ""messagetitle2"", ""content"": """", ""url"": ""http://web.abc/portal/hldjh/201404/t20140423_73322.htm"" } ] } }";
/// <summary> /// DataTable 转换为List 集合 /// </summary> /// <typeparam name="TResult">类型</typeparam> /// <param name="dt">DataTable</param> /// <returns></returns> public static List<TResult> ToList<TResult>(DataTable dt) where TResult : class, new() { //创建一个属性的列表 List<PropertyInfo> prlist = new List<PropertyInfo>(); //获取TResult的类型实例 反射的入口 Type t = typeof(TResult); //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); }); //创建返回的集合 List<TResult> oblist = new List<TResult>(); foreach (DataRow row in dt.Rows) { //创建TResult的实例 TResult ob = new TResult(); //找到对应的数据 并赋值 prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); }); //放入到返回的集合中. oblist.Add(ob); } return oblist; }
var result = from account in db.PublicAccount.ToList() where account.Name.Contains(keyword) select account; result = result.ToList(); JObject json = new JObject( new JProperty( "accounts", new JArray(//帐号数组 result.Select( r => new JObject(//账号对象 new JProperty( "id", r.ID ), new JProperty( "name", r.Name ), new JProperty( "desc", r.Desc ) ) ) ) ) );
标签:des style blog http color os
原文地址:http://www.cnblogs.com/yy2056/p/3851608.html