Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, Lambda expressions A lambda expression is a convenient way of defining an anonymous (unnamed) function that can be passed around as a variable or as a parameter to a method call. Many LINQ methods take a function (called a delegate) as a parameter. ... The => operator is called the "lambda operator". Learn More,

How to convert list of lists to single list in c# using lambda expression?

Description:I have records coming from database where each item can have multiple TagIds(List<string>) assigned to it. I have combined the lists but currently it has become a list of lists as shown below,


List<List<string>> listOfLists=databaseQueriedItems;

but I need the items in one list like following one(i.e I want to flatten the hierarchy)

List<string> singleListOfStrings;

Posted by: | Posted on: Aug 29, 2020

1 answers

Replies

1

This can be easily handled in one line of code by using the SelectMany method as shown below.

List<string> singleListOfStrings= databaseQueriedItems.SelectMany(dqi => dqi.TagIds).ToList()

Replied by: | Replied on: Aug 29, 2020



Reply