Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:56:23+00:00 2026-06-17T14:56:23+00:00

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

  • 0

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();
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:56:24+00:00Added an answer on June 17, 2026 at 2:56 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Linq help - Sql trace returns result, but datacontext returning null Question
Possible Duplicate: How to Select Min and Max date values in Linq Query I'll
Possible Duplicate: Linq: “Or” equivalent of Where() I posted a question about a week
Possible Duplicate: How do I use LINQ Contains(string[]) instead of Contains(string) Suppose I have
Possible Duplicate: check whether a List<string> contains an element in another List<string> using LINQ
Possible Duplicate: LINQ to XML. How to get some string? My XML Response look
Possible Duplicate: LINQ Between Operator Dear All, Hi, I need to write this query
Possible Duplicate: LINQ to append to a StringBuilder from a String[] Forgive my functional
Possible Duplicate: Are IEnumerable Linq methods thread-safe? quick question. Will using Where,Single,SingleorDefault on a
Possible Duplicate: Concat all strings inside a List<string> using LINQ I am using C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.