Geeks With Blogs
Abhishek Anand Bits & Bytes about .net technology framework. linq
Deferred Execution Vs Eager Loading
Lets start with an Example :int[] listOfNumbers = new int[] { 5, 9, 8, 1, 7, 3, 6, 4, 2, 0 }; int i = 0; var query = from num in listOfNumbers select ++i; // Note, the local variable 'i' is not incremented until each element is evaluated (as a side-effect): foreach (var x in query) { Console.WriteLine("x = {0}, i = {1}", x, i); } Now without deferred execution we would expect that after the variable “query” is declared, “i” would have the value 10 and the output would be: “x = {1-10}, i = 10” However ......

Posted On Tuesday, December 3, 2013 2:41 PM

LINQ performace considerations
Outside of LINQ statements such as .Any(predicate) and .First(predicate) short circuiting execution when a matching element is found, the library does not inherently do any underlying optimizations due to the limits of what is available to it. Since LINQ only works with IEnumerables, the only thing that can be done is to loop through the elements in the collection, therefore, no index access, saving of local properties such as Length, etc. is possible. It is important to keep in mind that each time ......

Posted On Thursday, November 21, 2013 4:30 PM

Deferred Execution with LINQ
One of the best features of LINQ is the fact that it is lazy. Lazy or Deferred Execution allows you to specify all the conditions of your LINQ statement (Where, OrderBy, Select, etc), without actually executing those functions on the given collection until the results are needed. An example of where you can see this lazy execution in action is when using the Visual Studio Debugger. Let’s say you have an IEnumerable<int> that you want to execute a LINQ statement on and inspect the resulting ......

Posted On Monday, November 18, 2013 6:56 PM

LINQ in Performance Intensive Situations
As most of you know LINQ provides a set of great extension methods for common functions on .NET Collections. In most cases LINQ is simply the “prettier”, abstracted out implementations of the lower level .NET language features like loops and conditionals. Even though LINQ often increases readability, it does come with a little more overhead. This additional overhead is negligible in most cases, but when performance is a primary concern in your function every millisecond counts. In these scenarios, ......

Posted On Monday, November 18, 2013 6:42 PM

Filtering Duplicates from a Collection with LINQ
Often times there are requirements to ensure there are no duplicate entries in a collection. If you are familiar with SQL you would probably immediately think of the “distinct” keyword. LINQ also provides a Distinct() method that uses an objects Equals() method to test equality. IEnumerable<int> intArr = new[] { 1, 2, 2, 3 }; intArr = intArr.Distinct(); //Has size 3 This works great for value types, common reference types (like string), and for classes where you have access to override the ......

Posted On Monday, November 18, 2013 6:34 PM

Flattening Collection Hierarchies with LINQ
Ever run into the situation where you want to retrieve a single collection of objects from a hierarchy like the one below? IEnumerable<ClassA> { ClassA{ IEnumerable<ClassB> } } The first thing most people try is to use the LINQ Select () statement like so: var classBCollection = classACollection.Select(a => a.ClassBCollection); But when inspecting the type of classBCollection, we see it has type IEnumerable<IEnumerable&... and NOT the desired type of simply IEnumerable<ClassB>. ......

Posted On Monday, November 18, 2013 6:33 PM

.Single() vs. .SingleOrDefault()
The goal of both the Single and SingleOrDefault methods is the same: get one element in the collection that matches the provided predicate. The difference between the two lies in their behavior when no matching element is found. The Single method will throw an error saying something like “Sequence contains no elements”, while the SingleOrDefault() method will return the default value of the collection’s type. For reference types and nullable, it will return null and for value types it will return ......

Posted On Monday, November 18, 2013 6:17 PM

.Any() vs. .Count() > 0
The Any method is an expression that will check the collection that was called on to see if there are “any” elements matching the given criteria. Criteria can be provided as a predicate (function/lambda expression resulting in a Boolean value) to the Any method as a parameter. When no parameters are provided, .Any() checks to see if there are elements in the collection. For this reason, .Any() is preferred over .Count() > 0 ......

Posted On Monday, November 18, 2013 6:13 PM

Copyright © Abhishek Anand | Powered by: