Linq 结果转成 json

  public string BindGrid(string page,string rows)
        {
            int pageIndex = 0;
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            int pageSize = 20;
            int totalcount = 0;
            string out_s = "";

            using (SmtDataContext db = new SmtDataContext())
            {
                using (var writer = File.AppendText(Log.getLogFile()))
                {
                    db.Log = writer;
                    var loadOptions = new DataLoadOptions();
                    db.LoadOptions = loadOptions;
                    string orderby = "order_no";

                        var q_count = from c in db.tb_orders select c;
                      

                        totalcount = q_count.Count();
                        //Skip(100)代表跳过100行后.从第101行开始分页;
                        //take(10)表示多少行分页;
                        // 第一种                    var aClass = q_count.OrderBy(p => GetPropertyValue(p, orderBy.SelectedValue.Trim())).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                        // 第2种
                        var aClass = LinqOrderBy.OrderByDescending(q_count, orderby).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        string json = serializer.Serialize(aClass);
                    //easyui 格式        {  "total":239, "rows":[ {"code":"010","name":"Name 10"} ]}   
                         StringBuilder sb = new StringBuilder();
                         sb.Append("{ \"total\":" + totalcount + ", \"rows\":" + json + "}");
                         out_s = sb.ToString();
                        writer.Close();
                }
            }
            return out_s;
        }

posted @ 2013-07-18 14:44  Bruce_yao  阅读(613)  评论(0编辑  收藏  举报