Wednesday, April 25, 2012

How to execute other methods while selecting in LINQ

Well, because I'm just still trying to master myself in LINQ, and i'm still working on pretty basic stuffs, I was looking at ways where I can do a select an array of BusinessDocument type without resorting to for-loop and still log the each of the document that I'm iterating to. Well, the below code is fantastic :

            // Modified to use LINQ for readability.
            IEnumerable<string> documents = oioublDocumentTypes.Select(documentName =>
                                                                               LogHandler.LogInfo("Requesting profile info for doc type: " + documentName.ToString());
                                                                               return documentName.ToString();

The above code is equaivalent to looping each of the documents from the oioublDocumentType array and then returning each of them as an IEnumerable of string. While doing that, log each to the file while iterating. This is just a single line of code which saves me from doing for-loop... Look's pretty cool...

So this means that you can execute a lot of method within the select before returning what you want to return as a result... you can even do calculation inside and return the correct calculation... pretty impressive..

1 comment:

  1. Gratitude for the giving out post How to execute other methods while selecting in LINQ love to read this whole new thinking blog post such a attractive posting,