score:28
.net 4 has a zip operator that lets you join two collections together.
var values = { "1", "hello", "true" };
var types = { typeof(int), typeof(string), typeof(bool) };
var objects = values.zip(types, (val, type) => convert.changetype(val, type));
the .zip method is superior to .select((s, i) => ...) because .select will throw an exception when your collections don't have the same number of elements, whereas .zip will simply zip together as many elements as it can.
if you're on .net 3.5, then you'll have to settle for .select, or write your own .zip method.
now, all that said, i've never used convert.changetype. i'm assuming it works for your scenario, so i'll leave that be.
score:5
object[] objects = values.select((s,i) => convert.changetype(s, types[i]))
.toarray();
score:6
assuming both arrays have the same size:
string[] values = { "1", "hello", "true" };
type[] types = { typeof(int), typeof(string), typeof(bool) };
object[] objects = values
.select((value, index) => convert.changetype(value, types[index]))
.toarray();
Source: stackoverflow.com
Related Query
- How to concatenate two collections by index in LINQ
- How can I combine this code into one or two LINQ queries?
- How to use LINQ to combine two or more collections into one collection
- How to Join two dictionary collections linq query
- How to concatenate two columns in linq to sql query's select projection
- How to Compare two IEnumerable Collections using LINQ
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- How to combine, remove duplicates, and sort two collections into third collection with linq
- how concatenate multiple rows in LINQ with two tables?
- How do I concatenate two arrays in C#?
- How to use LINQ with dynamic collections
- How to select values within a provided index range from a List using LINQ
- How do you add an index field to Linq results
- How are people unit testing code that uses Linq to SQL
- How to merge a collection of collections in Linq
- How to search between two dates in LINQ to Entity?
- How do I just LINQ Join() to link two IQueryables?
- Concatenate two column values in LinQ Lambda Expression
- C# How to split a List in two using LINQ
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to Union two Queries in LINQ to Fluent NHibernate?
- How do I add two lists in Linq so addedList[x] = listOne[x] + listTwo[x]?
- How can I access the loop index inside a LINQ select?
- How do I combine these two linq queries into a single query?
- using LINQ how can i concatenate string properties from itesm in a collection
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to intersect two different IEnumerable collections
- How to modify only one or two field(s) in LINQ projections?
- How can I get LINQ to return the index of the object which has the max value in a collection?
More Query from same tag
- Generic lists: The type arguments for method cannot be inferred from the usage in lambdas
- Creating a LINQ select from multiple tables
- Recursive LINQ calls
- Reverse result from SelectMany in Rx.net
- Get specific value from nested collections using LINQ
- Linq query is not working while sql is working properly
- Add minutes from one column to datetime column in linq query
- find list of one item not in second list
- Microsoft CRM 4.0 LINQ - Getting attribute displaynames from CRM with LINQ
- Cannot deserialize the current JSON array
- Linq-to-SQL strange behaviour
- How can I merge the results of a group by Linq-to-XML query?
- Write a LINQ in C# to filter max x/y in generic PointF list
- Using 2 where clauses with Entity Framework
- How To Get Last Inserted Row From Table Using LINQ?
- Using Generics in Linq Query?
- Default excluding condition and extension to include them back
- IQueryable losing the ORDER BY
- SQL Connection Remains Open when using DataContext
- Remove one list from another mvc
- Querying a complex Json with Linq
- LINQ to check if class is in list
- Strongly typed Linq filtering method
- C# Filter XML for Attribute Name
- LINQ's Func<bool> is only called once?
- Remove multi indexes from linq list
- Linq Query With Multiple Joins Not Giving Correct Results
- Entity Framework Many to Many Filter
- Split custom list based on values in another list
- LINQ : Checking against each other item in a list