While my company's main projects are still .NET 2.0 or even .NET 1.1, I do work on some utilities in .NET 3.5. And I have found myself using LINQ more and more every day.
I quite like LINQ to SQL, but what I'm talking here is LINQ to Objects.
Here are some recent uses that save me quite a bit of code.
Get texts of selected items in ListView
1: var selectedAttachments = from ListViewItem item in listView1.SelectedItems select item.Text;
Get list of filenames in an array from a collection of objects
1: var attachments = from Outlook.Attachment attachment in selectedMail.Attachments
2: select attachment.FileName;
To sort unsorted collection so I can bind it to ComboBox and items appear sorted
1: var sortedUsers = from user in users.users
2: orderby user.fullname ascending
3: select user;
But sometimes LINQ can make you do stuff, your really don't need to do.
Concatenate strings in an array and separate them with ", "
1: args.Attachments.Aggregate((val1, val1) => val1 + ", " + val2)
There is an easier way, of course, as dr.T reminded me, while showing him my LINQ code. :)
1: string.Join(", ", args.Attachments)
Any tips and corrections are welcome. I'm still quite a n00b when it comes to LINQ!
Technorati Tags: LINQ