Using the type in linq

Possible Duplicate:
LINQ: Select parsed int, if string was parseable to int

This could be a basic question, but I couldn't figure out a work around. I have an array of strings and I tried to parse them with integers. As expected I got Format Exception.

How could I skip "3a" and proceed parsing the remaining array and storing the integers into output using Linq.? Is this a better approach or a DON'T DO practice? Pls shed some light on how to use TryParse in this case

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] values = { "1", "2", "3a","4" };
            List<int> output = new List<int>();

            try{
                output = values.Select(i => int.Parse(i)).ToList<int>();
            }
            catch(FormatException)
            {
                foreach (int i in output)
                    Console.WriteLine(i);
            }

            foreach (int i in output)
                Console.WriteLine(i);

            Console.ReadLine();
        }

    }
}


You can use int.TryParse

string[] values = { "1", "2", "3a","4" };
int i = int.MinValue;
List<int> output = values.Where(s => int.TryParse(s, out i))
                         .Select(s => i)
                         .ToList();

Demo

However, Eric Lippert would not be amused. So if you don't want to (ab)use side effects, this would be the best-practise approach:

Create an extension method like:

public static class NumericExtensions
{
    public static int? TryGetInt(this string item)
    {
        int i;
        bool success = int.TryParse(item, out i);
        return success ? (int?)i : (int?)null;
    }
}

Then you are able to write this:

List<int> output = values.Select(s => s.TryGetInt())
             .Where(nullableInt => nullableInt.HasValue)
             .Select(nullableInt => nullableInt.Value)
             .ToList();