none
How to make multiple List<T>? RRS feed

  • Question

  • I made List<Message> messagelist = new List<Message>.

    Message is a class I made.

    For example, Sam's messagelist, Tom's messagelist, Jane's messagelist ......

    Each person's messagelist can be removed and new person's messagelist can be added.

    I need to add name to each messagelist to find later.

    How to make data structure for this?

    Friday, January 3, 2020 3:50 AM

All replies

  • I suggest making a class called MessageList, then have a list of those.

    public class MessageList
    {
       // The person this lest refers to, like John, Fred, Sally etc.
       public string Name{get; set;}
    
       // The person's messages.
      public  List<Message> Messages = new List<Message>();
    
       // etc. etc. functionality and constructors go here.
    }
    
    
    
    // Use like so.
    List<MessageList> allMessages = new List<MessageList>();
    
    MessageList ml = new MessageList();
    ml.Name = "Fred";
    Message message = new Message(); // Or whatever the constructor looks like.
    ml.Messages.Add(message);
    
    allMessages.Add(ml);
    
    foreach(MessageList mlist in allMessages)
    {
       // etc.
    }
    

    Friday, January 3, 2020 5:05 AM
  • Hi,

    In addition to Ante's solution, you can also consider using a Dictionary.

                Dictionary<string, List<Message>> keyValuePairs = new Dictionary<string, List<Message>>();
                List<Message> messages = new List<Message>();
                messages.Add(new Message() { Content = "Tom's Message1" });
                messages.Add(new Message() { Content = "Tom's Message2" });
                keyValuePairs.Add("tom", messages);
    
                foreach (var item in keyValuePairs)
                {
                    Console.WriteLine(item.Key);
                    foreach (var values in item.Value)
                    {
                        Console.WriteLine(values.Content);
                    }
                }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 3, 2020 5:45 AM
  • We don't know if this is just a toy app, of course, but if you have plans for this to go into real-world use, then you should not think about storing all messages for everyone inside nested data structures.  Instead, you need to be using a database on disk.  When you are dealing with Sam, you'll go fetch Sam's messages from the database, and focus on that one user's list of messages.

    There are lots of easy-to-use databases for this kind of thing.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, January 3, 2020 7:06 PM
  • If this is more than something to learn then consider using a database e.g.

    Then in SQL

    DECLARE @UserId INT= 2;
    SELECT UserInformation.Id, 
           UserInformation.UserName
    FROM UserInformation
         INNER JOIN UserMessage ON UserInformation.Id = UserMessage.UserId
    WHERE dbo.UserInformation.Id  = @UserId

    .

    INSERT INTO dbo.UserMessage (UserId,  Message) VALUES (2,  'Some message');

    And so forth. 

    You can access data via a data provider or Entity Framework 6 or Entity Framework Core.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 3, 2020 8:36 PM
    Moderator
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 10, 2020 9:29 AM
  • Hello,

    >I need to add name to each messagelist to find later.

    Look on the basic OOP definition - objects are responsible for them self.

    Name not a part of the List<T> - it is a part of the Message - so definition of your task - search in the list for Message with specific Name.

    If you need several lists - just get several list-type variable or array or list of them.


    Sincerely, Highly skilled coding monkey.


    Friday, January 10, 2020 9:49 AM