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.
you can do that with a simple linq
int num = 0;
var ints = (from str in strings
where int.TryParse(str,out num)
select num).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With