Musings
Public · Protected · Private
LINQ convert string[] to Int[]
-
2012-07-18 12:20string[] numbers = { "0142", "010", "00219", "00927" }; // Get Numbers int[] nums = numbers .Select(s => Int32.Parse(s)) .ToArray(); foreach (int num in nums) Console.WriteLine(num); // Get Numbers in order nums = numbers .Select(s => Int32.Parse(s)) .OrderBy(s => s) .ToArray(); foreach (int num in nums) Console.WriteLine(num);
-
2012-07-18 12:28// Get Squares // add a method as below.. public int getsquare(int inn){ return inn * inn; } // now call as below int[] squares = numbers .Select(s => (getsquare(Int32.Parse(s)) ) ) // parse and get Square .OrderBy(s => s) // sort .ToArray() ; // to an array
-
2012-07-18 12:29foreach (int num in squares) Console.WriteLine(num);
-
2012-07-18 12:36// return an anonymous type object var znums = numbers .Select(s => (getsquare(Int32.Parse(s))).ToString().Substring(0, 1)) .OrderBy(s => s) .Select(s => new { firstMember = s, secondMember = ("%" + s + "%") }) .Select(r => new { aaa = r.firstMember, bbb = r.secondMember }).ToArray(); foreach (var num in znums) Console.WriteLine(num.aaa.ToString() + num.bbb.ToString()); Notice the Two .Select clauses and two lines... UNNECESSARY .. Shown to explain how the member names work. Try commenting the last SELECT line and notice the changes required while displaying on console.Writeline( ....)
This blog is frozen. No new comments or edits allowed.