法1:

第一种方法可以采用复制一个Datatable然后循环需要筛选的DataTable,循环出最新的十条,添加到复制的DataTable中。

 DataTable dt = ETMSoft.Cache.ETMCache.GetCacheService().RetrieveObject("/news/newscache") as DataTable;
         //    if (dt == null)
         //    {
         //        DataTable Newsdt = GetAllNews();
         //        CacheManage.NewsCache(Newsdt);
         //        DataView NewView = new DataView(Newsdt);
         //        NewView.RowFilter = "Menu_Pk='" + IntClass + "'";
         //        NewView.Sort = " News_Time  desc ";
         //        Newsdt = NewView.ToTable();
         //        DataTable dtv = new DataTable();
         //        dtv = Newsdt.Clone();  //复制一个dataTable
         //        for (int i = 0; i < Newsdt.Rows.Count&&i<10; i++)
         //        {
         //            DataRow dr = Newsdt.Rows[i];
         //            dtv.Rows.Add(dr.ItemArray);  //此处通过一个数组来添加和设置行的所有值.
         //        }
         //        return dtv;
         //    }
         //    else
         //    {
         //        DataView view = new DataView(dt);
         //        view.RowFilter = "Menu_Pk='" + IntClass + "'";
         //        view.Sort = " News_Time  desc ";
         //        dt = view.ToTable();
         //        DataTable dtv = new DataTable();
         //        dtv = dt.Clone();  //复制一个dataTable
         //        for (int i = 0; i < dt.Rows.Count&&i<10; i++)
         //        {
         //           DataRow dr = dt.Rows[i];
         //           dtv.Rows.Add(dr.ItemArray);
         //        }


         //        return dtv;

法2:上面第一种方法虽然可以用DataView实现刷选出最新的十条,但是由于需要复制一个DataTable并且还需要循环添加到另一个DataTable中,第一种方法并不理想,本来就是想利用缓存提高网站的性能,所以一直都想找到另外一种相对性能上比较的方法,原来的查阅了很多资料也没找到,后来无意之间想到了一种很简单的方法,提供给大家分享:

///////////////就是利用DataView.RowFilter筛选条件当数据超过十条的时候,按时间降序排列,只要用新闻发布时间大于第十条的新闻发布时间,DataView就可以筛选出最新的十条(这个方法简单吧!)

 public DataTable NoticePhotoNews(int IntClass)   ///////////////////////////////////////////////查询出数据创建缓存的时候按新闻发布时间降序,DataView筛选的的时候没必要再按时间降序排列一遍,这样可以提高性能
         {
             int s = 10;
             DataTable dt = ETMSoft.Cache.ETMCache.GetCacheService().RetrieveObject("/news/newscache") as DataTable;
             if (dt == null)
             {
             
                 DataTable Newsdt = GetAllNews();
                 CacheManage.NewsCache(Newsdt);
                 DataView NewView = new DataView(Newsdt);
     
                 NewView.RowFilter = "Menu_Pk='" + IntClass + "'";
                 if (NewView.Count >= 10)
                 {
                     NewView.RowFilter = "News_Time >  '" + NewView[10]["News_Time"].ToString() + "'";
                 }
                 Newsdt = NewView.ToTable();
            
                 return Newsdt;
             }
             else
             {
                 DataView view = new DataView(dt);
  
                 view.RowFilter = "Menu_Pk='" + IntClass + "'";
                 if (view.Count >= 10)
                 {
                     view.RowFilter = "News_Time >  '" + view[10]["News_Time"].ToString() + "'";
                 }
                 dt = view.ToTable();
     
                 return dt;
             }
         }

三、法三:可以用dt.Select()方法筛选出最新的十条代码如下:

        public DataTable NoticePhotoNews(int IntClass)   ///////////////////////////////////////////////查询出数据创建缓存的时候按新闻发布时间降序,DataView筛选的的时候没必要再按时间降序排列一遍,这样可以提高性能
         {
             int s = 10;
             DataTable dt = ETMSoft.Cache.ETMCache.GetCacheService().RetrieveObject("/news/newscache") as DataTable;
             if (dt == null)
             {
             
                 DataTable Newsdt = GetAllNews();
                 CacheManage.NewsCache(Newsdt);
                 DataView NewView = new DataView(Newsdt);
     
                 NewView.RowFilter = "Menu_Pk='" + IntClass + "'";
                 //if (NewView.Count >= 10)
                 //{
                 //    NewView.RowFilter = "News_Time >  '" + NewView[10]["News_Time"].ToString() + "'";
                 //}
                 Newsdt = NewView.ToTable();
                 
                 if (Newsdt.Rows.Count >= 10)
                 {
                     Newsdt.Select("News_Time >  '" + NewView[10]["News_Time"].ToString() + "'");
                 }
                 return Newsdt;
             }
             else
             {
                 DataView view = new DataView(dt);
  
                 view.RowFilter = "Menu_Pk='" + IntClass + "'";
                 //if (view.Count >= 10)
                 //{
                 //    view.RowFilter = "News_Time >  '" + view[10]["News_Time"].ToString() + "'";
                 //}
                 dt = view.ToTable();
                 if (dt.Rows.Count >= 10)
                 {
                     dt.Select("News_Time >  '" + view[10]["News_Time"].ToString() + "'");
                 }
                 return dt;
             }
         }

四、法四:如果.net版本是3.5以上可以采用ling查询DataSet(有待研究)

from m inMyTable
take
10
select m

 

posted on 2012-07-01 22:19  微笑点燃希望  阅读(3240)  评论(0编辑  收藏  举报
font=white