Philosophy of use? Here's a general answer you can apply elsewhere.
Read a few examples of where a tool is useful. If you then find yourself lacking a reason to use that tool, because similar examples never arise for you, then forget about it. It may just not be applicable to your domain.
If all the data you are manipulating is in the RDBMS, then maybe you don't need Linq to Objects at all.
On the other hand... it may be that you're thinking of it as a way of doing some extra manipulation on data from the database, and so missing opportunities to tighten up the expressiveness of your code that has nothing to do with the database.
Example: you're reading a file consisting of lines of plain text.
var lines = File.ReadAllLines(fileName);
As it happens, lines
now contains an array of strings, but arrays support IEnumerable, so we can use Linq methods on them. Suppose you want to remove the lines with nothing in them:
var nonBlankLines = lines.Where(line => line.Trim() == string.Empty);
And suppose you wanted those strings in quotes (naive implementation - need to escape existing quotes!):
var quoted = lines.Where(line => line.Trim() == string.Empty)
.Select(line => "\"" + line + "\"");
(I like to line up successive operations with the dot-method aligned under each other.)
And unless I'm going to do something else with the lines, I'd do this:
var quoted = File.ReadAllLines(fileName)
.Where(line => line.Trim() == string.Empty)
.Select(line => "\"" + line + "\"");
And then suppose I want this all turned into a single string separated by commas, there's a method in string called Join
that can do that, if I turn it all into an array first:
var quoted = string.Join(", ",
File.ReadAllLines(fileName)
.Where(line => line.Trim() == string.Empty)
.Select(line => "\"" + line + "\"")
.ToArray());
Or we can use a Linqy way of doing it:
var quoted = File.ReadAllLines(fileName)
.Where(line => line.Trim() == string.Empty)
.Select(line => "\"" + line + "\"")
.Aggregate((a, b) => a + ", " + b);
Also it's no big deal to fill in a few blanks where you find there's no existing operator for what you need (although sometimes there turns out to already be one). One big one that is missing is an opposite to Aggregate
, which I've taken to calling Util.Generate
:
IEnumerable<T> Generate<T>(T item, Func<T, T> generator)
{
for (; item != null; item = generator(item))
yield return item;
}
This comes in very handy when you've got a linked list, of the kind that crop up occasionally in object models. An example is Exception.InnerException
, which allows exceptions to form a linked list, with the innermost one at the end. Suppose we want to display the message from the innermost exception of x
only:
MessageBox.Show(Util.Generate(x, i => i.InnerException).Last().Message);
The Generate
helper method converts the linked list into an IEnumerable, allowing other Linq methods to work on it. It just needs to be given a lambda to tell it how to get to the next item from the current one.
Maybe this will get you started, or maybe you need more examples, or maybe you literally never manipulate any data that isn't sourced from the RDBMS.