During this series I will be investigating the purpose and uses of the Extension methods made available in the System.Linq namespace specifically for classes that implement IEnumerable. If you have any gems you have created for the extension in question drop a comment below as I would love to see what people are coming up with. In Part 3 I will be exploring the very simple Any extension method described in the MSDN documentation as follows.
“Determines whether a sequence contains any elements.” – MSDN
When I first started looking at Any it seemed really strange at first, the first override just returns true if the Enumeration is greater than 0 which quite frankly is bizarre since its a direct copy of Count. It would just prove a neater way to write Linq expressions rather than the alternative using Count.
var orders = new List<Order>()
{
new Order() {Total = 123.23},
new Order() {Total = 512},
new Order() {Total = 123.23},
new Order() {Total = 5172.34},
new Order() {Total = 647.518},
new Order() {Total = 368.180},
new Order() {Total = 12.84},
new Order() {Total = 947.25},
};
if (orders.Any())
{
//do something
}
if (orders.Count > 0)
{
//do something
}
Both achieve the very same thing the second override is really useful it allows you to test all the items in the Enumeration for at least one match condition.
if (orders.Any(order => order.Total > 900))
{
//do something
}
I am really struggling to come up with a good real world example of how/when to use Any so I wont at this time. But if you find yourself writing this code below, dont!
bool result = false;
foreach(var order in orders)
{
if (order.Total > 900)
{
result = true;
break;
}
}
if (result)
{
//do something
}

http://blogs.wsj.com/photojournal/2009/02/17/pictures-of-the-day-115/
Reminds me of this painting in a spooky way. I for one welcome my new Robot overlords.

http://upload.wikimedia.org/wikipedia/commons/7/73/God2-Sistine_Chapel.png
During this series I will be investigating the purpose and uses of the Extension methods made available in the System.Linq namespace specifically for classes that implement IEnumerable. If you have any gems you have created for the extension in question drop a comment below as I would love to see what people are coming up with. In Part 2 I will be exploring the very simple but powerful All extension method described in the MSDN documentation as follows.
“Determines whether all elements of a sequence satisfy a condition” – MSDN
This method is tricky to highlight its real value without using it with other methods and/or a linq query like below. Say we want to find all customers in a list where all of their orders are for more than $10.00.
var loyalCustomers = from customer in customers
where customer.Orders.All(order => order.TotalPrice >= 10)
select customer;
As you can see this is a powerful way of selecting from a list of parents based on the properties of children however it can also be used in an if statement.
var orderItems = new List<Product>()
{
new Product() { Name="Ball", Stock = 0},
new Product() { Name="Skipping Rope", Stock = 5},
new Product() { Name="Comic", Stock = 3},
new Product() { Name="Gum", Stock = 2}
};
if (!orderItems.All(product => product.Stock > 0))
{
OrderStock(orderItems);
}
That is it All is a very simple extension method that can used very quickly to test an entire Enumeration.
System.Linq.Enumerable.Aggregate- Better Know an Extension Method Part 1