Ask HN: How do you get better at using LINQ?
I'm trying to get into C# and I cannot get LINQ. I always end up using a loop or something else that could easily be represented as a LINQ query. How do I recognise when I can use LINQ?
I've never really struggled with the concept, so I can't point you to resources that "clicked" for me. However, since LINQ basically just implements basic functional collection handling mechanisms, a look at how other programming enviroments deal with the idea might be helpful. To name a few things based on the same principles:
- Lisp's map, filter, reduce functions and derivates of them
-jQuery
- C++'s std algorithm
- Python Generators (in compination with Lisp-style functions)
-SQL's SELECT statements
Maybe, seeing them used could give you better ideas on how to use LINQ.
In terms of the necessary mindset, I would try to shift the focus from "What can I do with LINQ" to "what can't I do".
LINQ is immensely powerful and generally seems to decouple collection handling code, so I'd generally write everything I can with LINQ statements and just factor the steps out that are not feasible or get too complicated.
If you struggle with starting such a statement, try to describe, what you want to do - in steps performed on your collection(s) of input data:
"First, remove duplicates" -> Distinct()
"Then, only consider property X for each object in the collection" -> Select()
...and so on.
I had multiple Excel spreadsheets that to be pasted and sorted in a csv file >Initially I used queries in sqlite, but it would have taken way lower time to develop in linq.