Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. int asInt = 0;
  2.  
  3. var ints = from str in strings
  4. where Int32.TryParse(str, out asInt)
  5. select Int32.Parse(str);
  6.  
  7. var ints = strings.Select(str => {
  8. int value;
  9. bool success = int.TryParse(str, out value);
  10. return new { value, success };
  11. })
  12. .Where(pair => pair.success)
  13. .Select(pair => pair.value);
  14.  
  15. public static int? NullableTryParseInt32(string text)
  16. {
  17. int value;
  18. return int.TryParse(text, out value) ? (int?) value : null;
  19. }
  20.  
  21. var ints = from str in strings
  22. let nullable = NullableTryParseInt32(str)
  23. where nullable != null
  24. select nullable.Value;
  25.  
  26. int asInt = 0;
  27. var ints = from str in strings
  28. where Int32.TryParse(str, out asInt)
  29. select asInt;
  30.  
  31. strings.Select<string, Func<int,int?>>(s => (n) => int.TryParse(s, out n) ? (int?)n : (int?)null ).Where(λ => λ(0) != null).Select(λ => λ(0).Value);
  32.  
  33. public static class SafeConvert
  34. {
  35. public static int? ToInt32(string value)
  36. {
  37. int n;
  38. if (!Int32.TryParse(value, out n))
  39. return null;
  40. return n;
  41. }
  42. }
  43.  
  44. from str in strings
  45. let number = SafeConvert.ToInt32(str)
  46. where number != null
  47. select number.Value;
  48.  
  49. static int? ParseInt32(string s) {
  50. int i;
  51. if(int.TryParse(s,out i)) return i;
  52. return null;
  53. }
  54.  
  55. let i = ParseInt32(str)
  56. where i != null
  57. select i.Value;
  58.  
  59. var ints = strings.SelectMany(str => {
  60. int value;
  61. if (int.TryParse(str, out value))
  62. return new int[] { value };
  63. return new int[] { };
  64. });
  65.  
  66. var isInteger = new Regex(@"^d+$", RegexOptions.Compiled);
  67. var numbers = strings.Select(t => isInteger.Match(t))
  68. .Where(m => m.Success)
  69. .Select(m => Int32.Parse(m.Value));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement