日期:2014-05-20  浏览次数:20858 次

关于Linq to xml 分页的问题.
C# code
if (context.Request.RequestType == "POST")
        {
            //设置行为参数
            string orderString = (context.Request.Form["orderby"].ToString());//排序
            string order = "ascending";//排序:升序
            string orderBy = (!string.IsNullOrEmpty(orderString)) ? orderString.Substring(0, orderString.Length - 2) : "ProductNo";//要排序的字段,如果为空,默认为"ProductNo"
            if (orderString.EndsWith("_d"))
            {
                order = "descending";//排序:降序
            }
            int pageCount = int.Parse(context.Request.Form["pageCount"].ToString());//每页显示记录数
            int pageIndex = int.Parse(context.Request.Form["pageIndex"].ToString());//当前页
            int skipRecord = (pageIndex - 1) * pageCount;//跳过记录数

            //获取数据
            StockAccountModel model = new StockAccountModel();
            model.CompanyCD = companyCD;
            model.StartDate = context.Request.Form["txtStartDate"].Trim();
            //这里是把返回的DataTable转换为XML
            XElement dsXML = ConvertDataTableToXML(StockAccountBus.GetStockStructAnalysis(model, "", ""));
            //linq排序
            var dsLinq =
                (order == "ascending") ?
                (from x in dsXML.Descendants("Data")
                 orderby x.Element(orderBy).Value ascending
                 select new DataSourceModel()//下面有这个Model
                 {
                     
                     ProductNo = x.Element("ProductNo").Value,
                     ProductName = x.Element("ProductName").Value,
                     Specification = x.Element("Specification").Value,
                     UnitID = x.Element("UnitID").Value,
                     ProductCount = x.Element("ProductCount").Value,
                     TaxTotalPrice = x.Element("TaxTotalPrice").Value,
                     StockBizhong = x.Element("StockBizhong").Value,
                     ZanYaTotalPrice = x.Element("ZanYaTotalPrice").Value,
                     OutCountPerDay = x.Element("OutCountPerDay").Value,
                     OutSellCountPerDay = x.Element("OutSellCountPerDay").Value,

                 })
                          :
                (from x in dsXML.Descendants("Data")
                 orderby x.Element(orderBy).Value descending
                 select new DataSourceModel()
                 {
                     ProductNo = x.Element("ProductNo").Value,
                     ProductName = x.Element("ProductName").Value,
                     Specification = x.Element("Specification").Value,
                     UnitID = x.Element("UnitID").Value,
                     ProductCount = x.Element("ProductCount").Value,
                     TaxTotalPrice = x.Element("TaxTotalPrice").Value,
                     StockBizhong = x.Element("StockBizhong").Value,
                     ZanYaTotalPrice = x.Element("ZanYaTotalPrice").Value,
                     OutCountPerDay = x.Element("OutCountPerDay").Value,
                     OutSellCountPerDay = x.Element("OutSellCountPerDay").Value,

                 });
            int totalCount = dsLinq.Count();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("{");
            sb.Append("totalCount:");
            sb.Append(totalCount.ToString());
            sb.Append(",data:");
            sb.Append(ToJSON(dsLinq.Skip(skipRecord).Take(pageCount).ToList()));//转换为Json返回
            sb.Append("}");
            context.Response.ContentType = "text/plain";
            context.Response.Write(sb.ToString());
            context.Response.End();
        }
    }

    /// <summary>
    /// datatabletoxml
    /// </summary>
    /// <param name="xmlDS"></param>
    /// <returns></returns>
    private XElement ConvertDataTableToXML(DataTable xmlDS)
    {
        StringWriter sr = new StringWriter();
        xmlDS.TableName = "Data";
        xmlDS.WriteXml(sr, System.Data.XmlWriteMode.IgnoreSchema, true);
        string contents = sr.ToString();
        return XElement.Parse(contents);
    }

    public static string ToJSON(object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //数据源结构
    public class DataSourceModel
    {
        public string ProductNo { get; set; }
        public string ProductName { get; set; }
        public string Specification { get; set; }
        public string UnitID { get; set; }
        public string StorageName { get; set; }
        public string ProductCount { get; set; }
        public string TaxTotalPrice { get; set; }
        public string StockBizhong { get; set; }
        public string ZanYaTotalPrice { get; set; }
        public string OutCountPerDay { get; set; }
        public string OutSellCountPerDay { get; set; }
    }