Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int asInt = 0;
- var ints = from str in strings
- where Int32.TryParse(str, out asInt)
- select Int32.Parse(str);
- var ints = strings.Select(str => {
- int value;
- bool success = int.TryParse(str, out value);
- return new { value, success };
- })
- .Where(pair => pair.success)
- .Select(pair => pair.value);
- public static int? NullableTryParseInt32(string text)
- {
- int value;
- return int.TryParse(text, out value) ? (int?) value : null;
- }
- var ints = from str in strings
- let nullable = NullableTryParseInt32(str)
- where nullable != null
- select nullable.Value;
- int asInt = 0;
- var ints = from str in strings
- where Int32.TryParse(str, out asInt)
- select asInt;
- strings.Select<string, Func<int,int?>>(s => (n) => int.TryParse(s, out n) ? (int?)n : (int?)null ).Where(λ => λ(0) != null).Select(λ => λ(0).Value);
- public static class SafeConvert
- {
- public static int? ToInt32(string value)
- {
- int n;
- if (!Int32.TryParse(value, out n))
- return null;
- return n;
- }
- }
- from str in strings
- let number = SafeConvert.ToInt32(str)
- where number != null
- select number.Value;
- static int? ParseInt32(string s) {
- int i;
- if(int.TryParse(s,out i)) return i;
- return null;
- }
- let i = ParseInt32(str)
- where i != null
- select i.Value;
- var ints = strings.SelectMany(str => {
- int value;
- if (int.TryParse(str, out value))
- return new int[] { value };
- return new int[] { };
- });
- var isInteger = new Regex(@"^d+$", RegexOptions.Compiled);
- var numbers = strings.Select(t => isInteger.Match(t))
- .Where(m => m.Success)
- .Select(m => Int32.Parse(m.Value));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement