Sunday, 1 January 2012

Using Linq DefaultIfEmpty() extension method

By Using DefaultIfEmpty() extension method you can check the sequence for empty and return something for it and do some manipulation on it afterwards.

For example
var numbers = new int[] {1,2,3,4,5,6,7};
If the sequence is empty it will return 0 otherwise it will sum and return that.

By Default if you want to return some value other then 0 it will return that as well


In this case it will return 100 if the sequence is empty.

No comments:

Post a Comment