Recently I had an List of events that had been loaded from a CSV file. I then needed to filter this list of 5000 events down to anything happening in a single month, lets say April.
In the past I would have written (and I started to this time as well) a foreach loop that would loop through each event in the List, check the event dates to be occuring between April 1st and April 30th. If it does, add the event to a new List, if not move to the next. Then return that new List of Aprils events.
When I started to write this, I thought to myself, this would be easy if I could query it in SQL. Then the light bulb went off and I remembered this LINQ thing I'd been laying with. I created a var named qry, and selected from my event List all those whose event dates where between April 1st and April 30th. And ta-da, i just need to return qry.ToList() and I had my result set.
This also, in my opinion, resulted in cleaner and easier to read code. Unfortunately it will also probably have me trying to put LINQ style queries everywhere...
No comments:
Post a Comment