Part 9 Difference between Select and SelectMany in LINQ

12
1



Text version of the video

Slides

LINQ Tutorial – All Text Articles & Slides

LINQ Tutorial Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

Let us understand the difference between Select and SelectMany with an example.

In this example, the Select() method returns List of List[string]. To print all the subjects we will have to use 2 nested foreach loops.
IEnumerable[List[string]] result = Student.GetAllStudetns().Select(s =] s.Subjects);
foreach (List[string] stringList in result)
{
foreach (string str in stringList)
{
Console.WriteLine(str);
}
}

SelectMany() on the other hand, flattens queries that return lists of lists into a single list. So in this case to print all the subjects we have to use just one foreach loop.
IEnumerable[string] result = Student.GetAllStudetns().SelectMany(s =] s.Subjects);
foreach (string str in result)
{
Console.WriteLine(str);
}

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

12 COMMENTS

  1. I don't understand why there are dislikes.. May be some aliens are doing this.. Kudvenkat sir.. I am very grateful to you sir.. You are like Sachin in the field of cricket..🙏🙏🙏🙏

  2. Can you help ?
    I am traning my programing:

    writing a server usse system

    Server: 
                    Int32 port = 13000;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                    server = new TcpListener(localAddr, port);

    client
                    Int32 port = 13000;
                    TcpClient client = new TcpClient("127.0.0.1", port);

    When I run the clent this wkat responce i get.

    ……………………………………………………………………………..
    SocketException: System.Net.Sockets.SocketException (0x80004005): No connection
    could be made because the target machine actively refused it 127.0.0.1:8080
       at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
       at Con_Ms_User.Program.Connect(String server, String message) in d:UWSTest
    OrcB00231110ALLCon Ms UserCon Ms UserProgram.cs:line 26
    ……………………………………………………………………………..

    also runing xampp at port 8080 does not work.

LEAVE A REPLY

Please enter your comment!
Please enter your name here