To answer the edited question: in short, it is beneficial to use LINQ whenever you have to implement "query" functionality (that's what the Q in LINQ stands for). Defining an exact domain is difficult, but it greatly simplifies a variety of tasks associated with extracting and manipulating data from collections.
To elaborate slightly, a lot of query functionality has been brought directly into the language (or rather, the various LINQ-implementors), so things like aggregations, ordering, grouping, filtering, projections, joins (and many more) are all handled for you. The LINQ-based solutions are also typically far shorter than if you were to implement them "by hand", and also communicate their intent far better.
A simple example which often helps convey the power of LINQ is to display the contents of a directory, grouped by extension. Run through a typical imperative implementation in your head - there will be lots of implementation details already at the outset. Perhaps we will use a Dictionary<String, List<String>>
to index the files by extension. Of course, we'll have to check if a key already exists, instantiate a list, add to it, etc. It might go something like:
Dictionary<string, List<string>> fileGroups = new Dictionary<string, List<string>>();
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory))
{
string extension = Path.GetExtension(file).ToLower();
if (!fileGroups.ContainsKey(extension))
{
fileGroups[extension] = new List<string>();
}
fileGroups[extension].Add(file);
}
Consider the LINQ equivalent:
var query = from file in Directory.GetFiles(Environment.CurrentDirectory)
group file by Path.GetExtension(file).ToLower();
Notice that the query itself is only 2 lines, certainly shorter than any imperative solution we could have come up with. It is also pretty readable; the signal-to-noise ratio is higher than it was with the first solution. For those that are new to LINQ, you'd output the results of that query as follows:
foreach (var fileGroup in query)
{
Console.WriteLine(String.Format("*** Files with extension: {0}", group.Key));
foreach (string file in fileGroup)
{
Console.WriteLine(file);
}
}
With more complex examples, the differences normally become even more vast (consider simply grouping by multiple fields, for example). So, to summarise, LINQ solves many "day to day" data querying problems in a way which is often shorter and more self-descriptive. This comes at a mild cost of having to learn the syntax and the technology, but the benefits greatly outweigh the negatives.