代码改变世界

linq, List, datatable, Dictionary 模糊匹配速度

2011-09-23 11:32  yuejianjun  阅读(520)  评论(0编辑  收藏  举报
  public DataTable GetHotledt(string[] arrkey)
        {
            DataTable dt = getHoteldata();
            DataTable dt1 = new DataTable();
            DateTime dts = DateTime.Now;
          //  for (int i = 0; i < 500; i++)
          
//  {
                dt.DefaultView.RowFilter = strFilterSQL(arrkey);

                dt1 = dt.DefaultView.ToTable(); 
           // }
            Console.WriteLine("dt1: " + totalTime(dts, DateTime.Now));
            return dt1;
        }
     
        public List<SearchData> GetHotel(string[] arrkey)
        {
            //List<SearchData> list = GetDistrictList();
             
            List<SearchData> rList = new List<SearchData>();
            DateTime dts = DateTime.Now;
         //   for (int i = 0; i < 500; i++)
          
//  {
                var linqData = (from c in list
                                where (c.CnName.Contains(arrkey[0])) || (c.EngName.Contains(arrkey[0]))
                                select c)
                                ;

                rList = linqData.ToList<SearchData>();
         //   }
            Console.WriteLine("rList: " + totalTime(dts, DateTime.Now));
            return rList;
        }
        private string strFilterSQL(string[] arrkey)
        {
            StringBuilder sbFilter =new StringBuilder ();
            for (int i = 0; i < arrkey.Length; i++)
            {
                if (arrkey[i].Trim() != "")
                {
                    if (sbFilter.ToString() != "")
                    {
                        sbFilter.Append(" and ");
                    }
                    sbFilter.Append("( CnName like '%" + arrkey[i] + "%' or EngName like '%" + arrkey[i] + "%' )");
                }
            }
            return sbFilter.ToString ();
        }
        private static string totalTime(DateTime start, DateTime end)
        {
            TimeSpan ts = end.Subtract(start).Duration();
            return ts.TotalMilliseconds.ToString();
        }