先构造一个DataTable:
1 public DataTable GetDataTable() 2 { 3 DataTable dtTmp = new DataTable(); 4 dtTmp.Columns.Add("1", typeof(string)); 5 dtTmp.Columns.Add("2", typeof(string)); 6 dtTmp.Columns.Add("3", typeof(string)); 7 dtTmp.Columns.Add("4", typeof(string)); 8 dtTmp.Columns.Add("5", typeof(string)); 9 dtTmp.Columns.Add("6", typeof(string)); 10 dtTmp.Columns.Add("7", typeof(string)); 11 dtTmp.Columns.Add("8", typeof(string)); 12 13 for (int i = 0; i < 1000; i++) 14 { 15 DataRow dr = dtTmp.NewRow(); 16 17 dr["1"] = "数据" + i; 18 dr["2"] = "数据" + i; 19 dr["3"] = "数据" + i; 20 dr["4"] = "数据" + i; 21 dr["5"] = "数据" + i; 22 dr["6"] = "数据" + i; 23 dr["7"] = "数据" + i; 24 dr["8"] = "数据" + i; 25 26 dtTmp.Rows.Add(dr); 27 } 28 29 return dtTmp; 30 }
建立单元测试项目:
1 [TestClass] 2 public class UnitTest1 3 { 4 Class1 cs1; 5 DataTable dt; 6 public UnitTest1() 7 { 8 cs1 = new Class1(); 9 dt = cs1.GetDataTable(); 10 } 11 12 [TestMethod] 13 public void TestMethod0() 14 { 15 /*空测试项目*/ 16 } 17 18 [TestMethod] 19 public void TestMethod1() 20 { 21 DataTable dtt = dt.AsEnumerable().Where(o => o.Field<string>("1").Contains("0")).AsDataView().ToTable(); 22 } 23 24 [TestMethod] 25 public void TestMethod2() 26 { 27 DataTable dtt = (from a in dt.AsEnumerable() where a.Field<string>("1").Contains("0") select a).AsDataView().ToTable(); 28 } 29 30 [TestMethod] 31 public void TestMethod3() 32 { 33 dt.AsDataView().RowFilter = "[1] like '%0%'"; 34 DataTable dtt = dt; 35 } 36 37 [TestMethod] 38 public void TestMethod4() 39 { 40 DataTable dtt = dt.Select("[1] like '%" + "0" + "%'").CopyToDataTable(); 41 } 42 }
运行后看效率:
1000行数据量:
10000行数据量:
100000行数据量:
1000000行数据量:
DataTable无索引状态下测试,也是日常最常用的数据过滤方法之一,
再大就不测了,数据量太大放DataTable本来就不适合,可以考虑nosql了,效率吗,如上,仅供参考,欢迎拍砖.