/// <summary> /// Use LinqToSql to retrieve a collection of objects, then turn that into a /// distinct set (which might all be done within one SQL statement, explaining /// the performance) /// </summary> private static void FindDistinctCategoriesLinq() { var watch = new Stopwatch(); watch.Start(); var db = new SearchResultsContext(); int i = 0; // figure out how many distinct categories we can find in the entire table: var distinctCategories = (from sr in db.SearchResults select new {sr.ResultCategoryID}).Distinct(); foreach (var cat in distinctCategories) { i += cat.ResultCategoryID; } long result = watch.Stop(); ReportResult(watch, result); }
/// <summary> /// Get a plain old DataReader, then feed that into the DBContext to retrieve /// objects, then operate on the collection of objects. /// </summary> private static void FindDistinctCategoriesDataReaderLinq() { var watch = new Stopwatch(); watch.Start(); using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); int i = 0; var cmd = new SqlCommand("SELECT * FROM SearchResults", conn); using (SqlDataReader dr = cmd.ExecuteReader()) { var db = new SearchResultsContext(); var results = db.Translate<SearchResult>(dr); // figure out how many distinct categories we can find in the entire table: var distinctCategories = (from sr in results select new { sr.ResultCategoryID }).Distinct(); foreach (var cat in distinctCategories) { i += cat.ResultCategoryID; } } } long result = watch.Stop(); ReportResult(watch, result); }