How to LINQ List<List<object>> and flatten it using SelectMany?

linq where condition in list c#
c# linq select from list of objects
linq query to select data from list
linq select new
linq select all
c# linq where in list lambda
c# lambda select where in list
linq select from list where in another list
class Player
    public int ID { get; set; }
    public string Name { get; set; }

void Main()
    List<object> cricketPlayers = new List<object>();
    cricketPlayers.Add(new Player { ID = 1, Name = "Dhoni" });
    cricketPlayers.Add(new Player { ID = 2, Name = "Kohli" });
    cricketPlayers.Add(new Player { ID = 3, Name = "Gibbs" });
    cricketPlayers.Add(new Player { ID = 4, Name = "Ponting" });
    cricketPlayers.Add(new Player { ID = 5, Name = "Flintoff" });

    List<object> footBallPlayers = new List<object>();
    footBallPlayers.Add(new Player { ID = 6, Name = "Ronaldinho" });
    footBallPlayers.Add(new Player { ID = 7, Name = "Messi" });
    footBallPlayers.Add(new Player { ID = 8, Name = "Ronaldo" });

    List<object> tennisPlayers = new List<object>();
    tennisPlayers.Add(new Player { ID = 9, Name = "Federer" });
    tennisPlayers.Add(new Player { ID = 10, Name = "Nadal" });

    List<object> players = new List<object>();


Please click here to view the Result image

I executed the above C# program in LINQPad and got the result given in the image. I need to write a Lambda Expression LINQ to get only the ten player objects (SelectMany???). Please let me know how to do this through LINQ (not using loop).


I get the 'players' list from an API. I am unable to change the API to return the players list in a different way.

Since you have a List<object> that actually contains List<List<Player>> and you want to get a list of players in one big list. Unfortunately for some external reason you are unable to change the API to give you sensibly typed data, you need to go through a couple of hoops. So given this:

List<object> players = <snip>;

You can extract a list of Player objects using SelectMany with some casting:

var allPlayers = players
    .SelectMany(list => (List<object>)list)

How to convert LINQ query result to List?, LINQ select items from a list within a list | Test your C# code online with .NET Fiddle code editor. When using LINQ to query non-generic IEnumerable collections such as ArrayList, you must explicitly declare the type of the range variable to reflect the specific type of the objects in the collection. For example, if you have an ArrayList of Student objects, your from clause should look like this:

If that's all information you need, then you just have to select the lists each by itself to concat them to a flatten list.

players.SelectMany(list => list);

I would recommmend to group them (with .GroupBy()) or give them some more information, because when you flatten the list you lose the information which player is playing what sport - this information is currently only part of the list.

LINQ select items from a list within a list | C# Online Compiler, The following query returns list students group by StandardID: Example: LINQ GroupBy Query - C#. var studentsGroupByStandard = from s in studentList group s  To cache the deck of cards, you can leverage the LINQ methods ToArray and ToList; when you append them to the queries, they'll perform the same actions you've told them to, but now they'll store the results in an array or a list, depending on which method you choose to call. Append the LINQ method ToArray to both queries and run the program again:


List<Players> players = new List<Players>();

Then they will all be in the same list

Sample LINQ Queries, Following is the syntax of writing LINQ queries on list or collection to get the required elements. C# Code. var result = from e in objEmp. select new. List<course> = (from c in obj.tbCourses select new course(c)).toList(); You can convert the entity object to a list directly on the call. IMHO this is the best answer. It is not only single command, but it also illustrates that from

Assuming you want all of the player objects in a single list, you could do something like this:

var players = cricketPlayers.Concat(footBallPlayers).Concat(tennisPlayers);

Your lists should probably also be of type Player, not object.

LINQ to Lists / Collections, In this blog we will see how linq query result can be converted into list and later how the list can be consumed according to the any requirement  My question is pretty simple, how can I do this in one statement so that instead of having to new the empty list and then aggregate in the next line, that I can have a single linq statement that outputs my final list. details is a list of items that each contain a list of residences, I just want all of the residences in a flat list.

Would something like this be a better approach?

enum Sport

class Player
    public int ID { get; set; }
    public string Name { get; set; }
    public Sport Sport { get; set; }

void Main()
    List<Player> players = new List<Player>();

    players.Add(new Player { ID = 1, Name = "Dhoni", Sport = Sport.Cricket });
    players.Add(new Player { ID = 2, Name = "Kohli", Sport = Sport.Cricket });
    players.Add(new Player { ID = 3, Name = "Gibbs", Sport = Sport.Cricket });
    players.Add(new Player { ID = 4, Name = "Ponting", Sport = Sport.Cricket });
    players.Add(new Player { ID = 5, Name = "Flintoff", Sport = Sport.Cricket });

    players.Add(new Player { ID = 6, Name = "Ronaldinho", Sport = Sport.Fooball });
    players.Add(new Player { ID = 7, Name = "Messi", Sport = Sport.Fooball });
    players.Add(new Player { ID = 8, Name = "Ronaldo", Sport = Sport.Fooball });

    players.Add(new Player { ID = 9, Name = "Federer", Sport = Sport.Tennis });
    players.Add(new Player { ID = 10, Name = "Nadal", Sport = Sport.Tennis });

    players.Select(p => p.Name).Dump();
    players.Where(p => p.Sport == Sport.Fooball).Select(p => p.Name).Dump();

Convert LINQ Query to List, For instance, you can query a generic List type containing a few hundred integer values and write a LINQ expression to retrieve the subset that  With LINQ to SQL, you first create an object-relational mapping at design time either manually or by using the LINQ to SQL Tools in Visual Studio. You write your queries against the objects, and at run-time LINQ to SQL handles the communication with the database.

Using LINQ With C#, Example. var list = from something in someList select x; // but change one property. This post is based off of my Stack  This walkthrough demonstrates the C# language features that are used to write LINQ query expressions. The following instructions are for Visual Studio. If you are using a different development environment, create a console project with a reference to System.Core.dll and a using directive for the System.Linq namespace. Start Visual Studio.

LINQ: Select an object, but change some properties without creating , Try this: Hide Copy Code. int[] ids= new int[]{1, 2, 4}; var t=mystudent.tablea.​Where(x=> ids.Any(y=>y == x.Id)).ToList();. So, instead of Contains as a replacement of the ???? in your code you can use Any which is more LINQ-uish: // Filter the orders based on the order status var filteredOrders = from order in orders.Order where new[] { "A", "B", "C" }.Any(s => s == order.StatusCode) select order;

How to linq contains string list in C#?, Then we add the new employee object to our Employee list. Once we're looping, we return the collection of employee objects, filtered to only  The reason you have to convert your collection to a List is because the only thing you know about the collection returned by LINQ's Where clause is that it produces a collection that implements the IEnumerable interface.