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

  • Home
  • SEARCH
  • 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 8027519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:50:17+00:00 2026-06-04T23:50:17+00:00

I am used to writing C# without using Linq or Lambda statements, but I’d

  • 0

I am used to writing C# without using Linq or Lambda statements, but I’d like to improve my understanding. I have code that looks like this written in C# 2.0 with a foreach loop:

        List<string> strings = new List<string>();
        strings.Add("1");
        strings.Add("blah");

        List<int> ints1 = new List<int>();
        foreach (string x in strings)
        {
            int r;
            if (int.TryParse(x, out r))
                ints1.Add(r);
        }

It’s doing a simple task – populating a list of ints from a list of strings, ignoring any that aren’t actually ints.
In my limited experience, Linq and Lambda statements seem to be able to cut foreach statements down into very succinct and readable ways of doing the same thing. So I thought I’d try this little one using C# 3.0. But the best I could come up with is this:

       IEnumerable<int> ints2 = strings.Select(x =>
                                     {
                                         int r;
                                         if (int.TryParse(x, out r))
                                             return r as int?;
                                         return null;
                                     })
                                     .Where<int?>(x => x != null)
                                     .Select(x => x.Value);

Yuck! I couldn’t find a way to combine the converting and filtering in one call and ended up with something that appears to be much worse than the original. Is there a way of doing this or should I stick with my foreach?

EDIT: To summarise findings, here are 2 ways to do this with LINQ if you are using .NET 4.0:

IEnumerable<int> ints1 = strings
.Where(x => new Int32Converter().IsValid(x))
.Select(x => int.Parse(x));//Broken in .NET 3.5 due to bug in Int32Converter


IEnumerable<int> int2 = strings
.Where(x => { int r; return int.TryParse(x, out r); })
.Select(x => int.Parse(x));

The second method is faster and is not broken in .NET 3.5. And there is probably no reason not to put this inside an extension method, as per the accepted answer, instead of the foreach.
See connect issue for the Int32Converter bug.

  • 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-04T23:50:19+00:00Added an answer on June 4, 2026 at 11:50 pm

    I would define my own extension method as follows:

    public static IEnumerable<int> AsIntegers (this IEnumerable<string> strings) {
       foreach (var s in strings) {
          int r; if (int.TryParse (s, r)) yield return r;
       }
    }
    
    ...
    
    List<int> intList = new List (stringList.AsIntegers ());
    

    or

    var intList = stringList.AsIntegers ().ToList ();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some class and it wont compile without using System.Linq. But i
I've used NUnit for years but have always been annoyed that you're forced to
I am having trouble writing the proper LINQ syntax. I have used it before
I am used to writing unit tests in Zend Framework 1.9 using PHPUnit_Framework_TestCase for
Coming from a PHP background, I'm used to writing small functions that return a
I am writing a stored procedure that when completed will be used to scan
I'm writing a Firefox extension that's to be used in house. Basically, I need
I'm writing a wrapper application that will be used to provide an abstraction layer
My application I have an application design that looks like this: web application layer
I am writing an application using openFrameworks, but my question is not specific to

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.