首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关于Linq to xml 分页的有关问题

2012-02-21 
关于Linq to xml 分页的问题.C# codeif (context.Request.RequestType POST){//设置行为参数string o

关于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; }    } 




现在有两个疑问

1,就是数字字段排序的时候,他不能按数字来排序,他把数字当做字符字段来处理,

有人讲,可能是我定义的//数据源结构
  public class DataSourceModel
全部是string型的,但是我好像该成Decemel也不行,可能是转换成xml的缘故。难道要用Linq to sql?

但是说在的我不懂linq,我是参考的。

2,还有一个疑问就是,我从后台返回出来的DataTable,不是直接从数据库直接查出返回的一个DataTable

而是通过程序控制了,

类似这种定义临时表,然后通过程序控制的。

C# code
 DataTable DTReturn = new DataTable();            DTReturn.Columns.Add("ProductNo");            DTReturn.Columns.Add("ProductName");            DTReturn.Columns.Add("Specification");            DTReturn.Columns.Add("UnitID");            DTReturn.Columns.Add("ProductCount");            DTReturn.Columns.Add("TaxTotalPrice");            DTReturn.Columns.Add("ZanYaTotalPrice");            DTReturn.Columns.Add("AllTotalPrice");            DTReturn.Columns.Add("StockBizhong");            DTReturn.Columns.Add("OutCountPerDay");            DTReturn.Columns.Add("OutSellCountPerDay");


或者大家能给我一个更好的思路。
而且我分页的话,我想直接查出10条记录,但是我是通过程序组成一个DataTable,烦啊。烦,

大家能明白我说什么不?

[解决办法]
参考
参考

热点排行