Preface
Description: Language INtegrated Query is a set of extensions for c # and Visual Basic languages.
Category: LINQ to Object, LINQ to XML, LINQ to SQL, LINQ to DataSet, and LINQ to ADO. NET.
Related: I believe that everyone is familiar with the linq, if not familiar with, you can refer to the MSDN address: http://msdn.microsoft.com/zh-cn/library/bb397933.aspx
Reason: Can I use it? In many cases, there are different opinions. Some people are confused, some people wait and see, some people think it doesn't matter, or some people say it's just a syntactic sugar, which is useless, which of the following are the mysteries of our series. First, let's take a look at the Efficiency Test of LINQ to Object in array filtering.
Instance analysis
Test environment: visual studio 2011 Beta (netframework 4.0 +) C # console Program
Test Requirement: Query more than 10000000 of the 100 data records.
Core code (LINQ ):
var linqList = from num in list1 where num > 100 select num;
Complete code:
/// <Summary> /// Efficiency Test /// </summary> /// <param name = "testCount"> Number of tests </param> private static void timeTest (int testCount) {const int listCount = 10000000; // array length Random random = new Random (); // random data build value // array build List <int> listData = new List <int> (); for (int I = 0; I <listCount; I ++) {listData. add (random. next (10000);} // LINQ test Stopwatch linq_Stopwatch = new Stopwatch (); linq_Stopwatch.Start (); Var linqList = from num in listData where num> 100 select num; var linqCount = linqList. count (); linq_Stopwatch.Stop (); // test Stopwatch before_Stopwatch = new Stopwatch (); before_Stopwatch.Start (); List <int> beforeList = new List <int> (listCount) in normal mode ); for (int I = 0; I <listData. count (); I ++) {if (listData [I]> 100) beforeList. add (listData [I]);} var beforeCount = beforeList. count; before_Stopw Atch. Stop (); // print the result Console. WriteLine (String. Format ("tested at {0}, test: {5} pieces of data. \ N \ r \ t: {1} millisecond for LINQ. {2} data records are filtered. \ N \ r \ t normal time: {3} milliseconds, filtered {4} data records. \ R \ n ", testCount, linq_Stopwatch.ElapsedMilliseconds, linqCount, before_Stopwatch.ElapsedMilliseconds, beforeCount, listCount ));}
Result
Conclusion: it can be seen that the efficiency of processing data by using LINQ to Object is better than that of manual judgment. Of course, the greatness of LINQ should be her concise and elegant syntax. When processing data, I personally recommend using LINQ to Object!