score:18
Accepted answer
If you know it is specifically a List<T>
and not another collection type then you can use List<T>.ConvertAll
:
Converts the elements in the current
List<T>
to another type, and returns a list containing the converted elements.
Example:
List<string> myObjectNames = myObjectList.ConvertAll(x => x.Name);
If you just know that it is an enumerable type but not necessarily a list then you can use the LINQ extension methods Enumerable<T>.Select
and Enumerable<T>.ToList
:
List<string> myObjectNames = myObjects.Select(x => x.Name).ToList();
Source: stackoverflow.com
Related Query
- Convert list of one type to list of another type in C# 3.5
- Convert a list of one type to another
- Create a list of one object type from a list of another using Linq
- How to convert nested object from one type to another
- Convert or map a class instance to a list of another one by using Lambda or LINQ?
- Copy a list of objects to another typed list in one line of code
- How to convert Collection of One Class type to Another Class Type collection in Entity Framework
- How do I get a list of integers that exist in one collection, but not in another of a different type using LINQ?
- C# Transform list from one type to another
- Convert one list to the other list type
- Can't convert one generic list to another
- Find items in one list based on items in another list of a different type
- linq lambda convert object with list of objects of the same type to another object
- Convert string[] to int[] in one line of code using LINQ
- Convert a list of objects to an array of one of the object's properties
- Sort one list by another
- LINQ - Find all items in one list that aren't in another list
- Convert CollectionBase to List or data type usable with Linq
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- Convert or map a list of class to another list of class by using Lambda or LINQ?
- Exclude items of one list in another with different object data types, LINQ?
- Automapper unable to project one enum type to another
- Check if one list contains any elements from another
- Check if a list contains all of another lists items when comparing on one property
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- LINQ Map one type to another
- Assign values from one list to another using LINQ
- How to convert Generic List<anonymous type > to Generic List <Classname>?
- LINQ Query: Determining if object in one list exists in another based on key
- List of Interfaces vs. List of Derived Type - Cannot Convert Expression Type to Return Type
More Query from same tag
- how to change sql query in example?
- How to convert Dictionary <string,List<string>> in C# to csv with keys as headers?
- How do you make a cast in Lambda expressions?
- LINQ query on DataGridViewSelectedCellCollection
- How to return a list of anonymous object in Linq to Sql on joining two tables
- How to fix this ArrayIndex error?
- AsQueryable() in Business logic layer - bad practice? (I think so but....)
- Linq to Entities Group By Order By Not Working
- Check if table exists in database but not in model
- What is the Equivalent of ObjectQuery.Parameters in DbQuery
- Losing foreach with Linq queries
- Entity Framework and ObservableCollections
- Linq Grouping Using Two Constraints
- linq selectmany flatten multiple levels
- Comparing Two models shared data types
- asp.net mvc Problems with simple LINQ statement with a list within a list
- Returning a nested dictionary within another dictionary with Linq
- Linq to Sql database query
- Linq to Datarow, Select multiple columns as distinct?
- How to return distinct result in linq to collection
- Linq that Aggregates distinct IDs
- Iterating over LINQ entity results
- MVC Viewmodel to display only model data
- Use Linq Expression object inside and outside query
- DocumentDb .NET Core SDK - IQueryable.OrderBy generates invalid query
- Syntax to execute code block inside Linq query?
- LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A
- c# Linq with complex result
- How to filter data from datatable according to columns suffix
- Extract values from XML with linq query