|It was hard for me too, but once you understand how the context shifts it becomes way easier. LINQ queries have a different flow from the rest of the imperative code and are best left a little separated from stuff like for loops and complex conditionals, they require you to use the same way of thinking you use when writing SQL. It's not that you are telling the machine what to do, you're telling it what you want done, so you read this:
As "Give me
field for all objects where
condition is true" instead of "For each object, if condition is true, stuff field on another list".
Truth is, those kind of LINQ queries are just convenience. The real power of LINQ comes when you start using joins and groups, like this example from LINQPad (still on the simple side):
var query =
from o in Orders
group o by o.ShipCountry into countryGroups
Country = countryGroups.Key,
from cg in countryGroups
group cg.ShipPostalCode by cg.ShipCity into cityGroups
City = cityGroups.Key,
PostCodes = cityGroups.Distinct()
that produces this output:
As much as I can write that using for loops and conditionas, i don't want to write it that way. Also, join's and group's are the only thing I'll ever use query syntax for, they are somewhat easier to understand that way.