Sunday 25 August 2013

LINQ - Usage of Convert All Method



In this article we are going to see a usage of convert all method. If we want to convert the data type of values present in the collection then we can do it through ConvertAll.



/* Convert All method */
   var results = new List<object>() { 1, 0, 1, 1, 0, "true" };
   List<bool> boolresults = results.ConvertAll(x => Convert.ToBoolean(x));
   foreach (bool res in boolresults)
             Console.WriteLine(res);



In this example we have a list of values which are in object type , so we are converting all to the boolean type. Now the value 1 and "true" are cast as Bool True. and 0 as False.

Output:

True
False
True
True
False
True

I hope from this you can understand the usage of ConvertAll() method.

No comments:

Post a Comment