Lambda expression to add objects from one list to another type of list
If you define constructor OtherObj(String name, Integer maxAge)
you can do it this java8 style:
myList.stream()
.map(obj -> new OtherObj(obj.getName(), obj.getMaxAge()))
.collect(Collectors.toList());
This will map all objects in list myList
to OtherObj
and collect it to new List
containing these objects.
How to convert list of object to another list object using streams?
Simple use map()
method in stream api and collect results:
List<Emp> employe = Arrays.asList(new Emp(1, 100), new Emp(2, 200), new Emp(3, 300));
List<Emp> employeRise = employe.stream()
.map(emp -> new Emp(emp.getId(), emp.getSalary * 100))
.collect(Collectors.toList());
employeRise.stream()
.forEach(s -> System.out.println("Id :" + s.getId() + " Salary :" + s.getSalary()));
Select a List from another List Using Lambda Expression
You're looking for SelectMany
to flatten a list of lists.
public List<Product> SelectManyExample(List<Products> products)
{
return products.SelectMany(prds => prds.ProductList).ToList();
}
Related Topics
Updating an Object from a List in C#
Declare Properties to Ignore in Entities Interface (Ef Core)
How to Close a File That Is Already Opened in a Directory
C# How to Check If a Url Exists/Is Valid
How to Cast a List into a Type Which Inherits from List<T>
How to Extract Custom Header Value in Web API Message Handler
Null Value on Xml Deserialization Using [Xmlattribute]
How to Map to a Dictionary Object from Database Results Using Dapper Dot Net
Convert Dictionary Values into Array
Thread.Sleep() Without Freezing the Ui
How to Convert Any Date Format to Yyyy-Mm-Dd
How to Secure the Asp.Net_Sessionid Cookie
How to Count Rows Within Entityframework Without Loading Contents
Read and Parse a Json File in C#
Restsharp Serializing Json Objects to Post Parameters
Get Value from Jtoken That May Not Exist (Best Practices)
How to Automatically Update an Application Without Clickonce