locked
LINQ : ToList() doesn't give me a concrete output RRS feed

  • Question

  • User-1370514677 posted

    Hi everyone, I'm new to the .NET Core world and C#.

    I wrote a simple console app in order to train with EFCore and LINQ :

    using System;
    using System.Linq;
    
    namespace DbProject
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Getting user input
                Console.WriteLine("Number of messages to register : ");
    
                int messages_number = 0;
    
                try
                {
                    messages_number = Convert.ToInt32(Console.ReadLine());
                }
                catch(Exception e)
                {
                    Console.WriteLine("ERROR while reading number of messages : ");
                    Console.WriteLine(e);
                }
    
                // Establishing a connection with the DB
                var db = new DataBaseContext();
    
                // Registering messages in DB
                for(int i = 0; i < messages_number; i++)
                {
                    Console.WriteLine("Add message : ");
    
                    string message_string = Console.ReadLine();
                        db.Add(new Message{Text=message_string});
                        db.SaveChanges();
                }
    
                // Getting the whole Message Table as a list
                var db_content = db.Message.ToList();
                    Console.WriteLine("List of all messages : ");
                    // Converting it to a String in order to print it from CMD
                    Console.WriteLine(db_content.ToString());
            }
        }
    }

    What I don't understand is that the final output of my app is :

    Number of messages to register :
    2
    Add message :
    Hello
    Add message :
    World
    List of all messages :
    System.Collections.Generic.List`1[DbProject.Message]

    Why don't I get "Hello" and "World" ?

    Thank you in advance

    Friday, November 13, 2020 11:35 PM

All replies

  • User475983607 posted

    db_context is an array.  You must iterate over the array.

    foreach(string s in db_content)
    {
        Console.WriteLine(s);
    }

    Friday, November 13, 2020 11:47 PM
  • User-1370514677 posted

    Hi @mgebhard ! Thanks for your answer.

    I tried the following but got an error :

                // Getting the whole Message Table as a list
                var db_content = db.Message.ToList();
                    Console.WriteLine("List of all messages : ");
                    // Converting it to a String in order to print it from CMD
                    foreach(string s in db_content)
                        Console.WriteLine(s);
    Program.cs(42,17): error CS0030: Cannot convert 'DbProject.Message' as 'string' 

    Any idea why ? :/

    Saturday, November 14, 2020 11:24 AM
  • User475983607 posted

    Any idea why ? :/

    Mt fault, db_context is an object not a string.

    foreach(Message s in db_content)
      Console.WriteLine(s.Text);

    Saturday, November 14, 2020 12:04 PM
  • User-1370514677 posted

    Perfect it works now !

    Thank you

    Saturday, November 14, 2020 12:24 PM