In Linq there are two kinds of queries you can execute on objects. Deferred and Non-Deferred. As expected, non-deferred queries will be executed immediately, deferred queries on the other hand will not be executed until iterate over the collection.
With that in mind take the following query
string[] servers = { "Server 1", "Server 2", "Dave" };
IEnumerable<string> results = servers.Where(server => Char.IsDigit(server[7]));
In this query we want to bring back the first two elements in our array which have a number at the 7th index, therefore we would get two results returned. However, although this code compiles, we have actually created a situation whereby a System.IndexOutOfRange exception will be thrown whenever this query is executed, this is because the server name Dave is only 4 chars long. This should be easy to find if we start using our results collection directly below our query but this may not always be the case. We may do the following.
string[] servers = { "Server 1", "Server 2", "Dave" };
IEnumerable<string> results = servers.Where(server => Char.IsDigit(server[7]));
ConnectToServer(results);
Our ConnectToServer method
public void ConnectToServer(IEnumerable<String> servers)
{
if(servers.Count > 0)
//Do Something
}
In this scenario, out System.IndexOutOfRange exception will be thrown from within the ConnectToServer method which may not have changed in years. By using linq are we possibly making it very difficult for ourselves when trying to track down errors?