score:682
Given an array you can use the Array.ConvertAll
method:
int[] myInts = Array.ConvertAll(arr, s => int.Parse(s));
Thanks to Marc Gravell for pointing out that the lambda can be omitted, yielding a shorter version shown below:
int[] myInts = Array.ConvertAll(arr, int.Parse);
A LINQ solution is similar, except you would need the extra ToArray
call to get an array:
int[] myInts = arr.Select(int.Parse).ToArray();
score:3
var list = arr.Select(i => Int32.Parse(i));
score:4
var asIntegers = arr.Select(s => int.Parse(s)).ToArray();
Have to make sure you are not getting an IEnumerable<int>
as a return
score:14
you can simply cast a string array to int array by:
var converted = arr.Select(int.Parse)
score:27
To avoid exceptions with .Parse
, here are some .TryParse
alternatives.
To use only the elements that can be parsed:
string[] arr = { null, " ", " 1 ", " 002 ", "3.0" };
int i = 0;
var a = (from s in arr where int.TryParse(s, out i) select i).ToArray(); // a = { 1, 2 }
or
var a = arr.SelectMany(s => int.TryParse(s, out i) ? new[] { i } : new int[0]).ToArray();
Alternatives using 0
for the elements that can't be parsed:
int i;
var a = Array.ConvertAll(arr, s => int.TryParse(s, out i) ? i : 0); //a = { 0, 0, 1, 2, 0 }
or
var a = arr.Select((s, i) => int.TryParse(s, out i) ? i : 0).ToArray();
var a = Array.ConvertAll(arr, s => int.TryParse(s, out var i) ? i : 0);
score:33
EDIT: to convert to array
int[] asIntegers = arr.Select(s => int.Parse(s)).ToArray();
This should do the trick:
var asIntegers = arr.Select(s => int.Parse(s));
Source: stackoverflow.com
Related Query
- Convert string[] to int[] in one line of code using LINQ
- Convert string to int array using LINQ
- Convert string to int for ordering using LINQ
- Convert to Int from String from LINQ Query field using C# for comparison (Height From)
- How to convert img url to BASE64 string in HTML on one method chain by using LINQ or Rx
- Check if a string has at least one number in it using LINQ
- Using LINQ to convert a list to a CSV string
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- LINQ lambda - convert int to string
- convert int to string in linq for searching
- Convert string array to custom object list using linq
- convert comma separated string to list using linq
- How to convert list of objects with two fields to array with one of them using LINQ
- Convert String To Int in LINQ
- Unable to convert string to int in LINQ query from MongoDB
- Convert two arrays to a new one using LINQ
- C# - Convert delimited string array w/ duplicates to dictionary using LINQ
- how to convert int to string in Linq to entities
- Convert string to int for sum in linq
- linq to entities can not convert int to string
- To determine if one of the Strings from a list contains the initial part of a specified string using LINQ
- Using Linq To Convert ListBox Items Values to int
- Convert delimited string to array and group using LINQ in C#
- Why can't cast int to string in LinQ using SqlFunctions?
- Dynamic Linq using Data Objects. How to convert Int32 to String for purpose of calling String.Contains()
- Linq Query on int using string
- Is it possible to have one LINQ in one line of code for
- Convert delimited string to xml in C# using LINQ
- how do i convert Dictionary string string to array of key value pair of string and string using linq and c#
More Query from same tag
- How to throuhly join two datatables using linq without column names
- Get value from property if not empty, otherwise get other property with Linq
- Client proxy is not creating methods
- What will be the equivalent sql query for this code?
- LINQ if in where condition
- Processing collection in sets
- Which LINQ statement is efficient for searching record using entity framework
- SQL to LINQ semi-complex query projecting to ViewModel
- How to select a sub collection available in a list of collection
- LINQ INNER JOIN with 2 inner joins
- Filter datatable if a column contains any one of the elements in an array
- LINQ query checks if a set contain two records with specific values?
- Make C# ParallelEnumerable.OrderBy stable sort
- LINQ optional where clause
- Filtering necessary data with LINQ
- RavenDB: How to do a simple map/reduce aggregation
- Issues with .NET Linq statement
- Expression Tree. Storing LambdaExpression with runtime types in Expression.Variable
- C# Lambda Method syntax to obtain attribute values that match pattern in LINQ to XML
- Linq Contains in one query
- SQL Server, LINQ, C# store data chain information
- Lambda OrderBy method
- Linq: Group by Time Interval
- How to get max sum range for specific time period from values in Dictionary
- How do I filter an array at codeigniter or php
- How to Pivote grid view in C#
- 'TimeOfDay' is not supported in LINQ to Entities - Find total seconds in Linq 2 SQL
- How to check in neat way if collection contains an object which's one property is equal to the object passed to Contains method?
- Distinct list by specified fields and ignoring other but return them too
- Select even/odd elements in IEnumerable<T>?