Q:

AspNet Categorize the messages by UserName when asking for Query

My database is structured as below
public partial class AspNetUserMessage
    {
        public string From { get; set; }
        public string To { get; set; }
        public string Text { get; set; }
        public System.DateTime SentDate { get; set; }
        public Nullable<System.DateTime> ReadDate { get; set; }
        public int Id { get; set; }
    }

I have an API point which returns all the messages sent by a user e.g
[
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "i love this backend!!",
        "SentDate": "2020-04-10T00:00:00",
        "ReadDate": "2020-04-10T00:00:00",
        "Id": 1
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "this is a sample msg",
        "SentDate": "0001-01-01T00:00:00",
        "ReadDate": null,
        "Id": 2
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "id test",
        "SentDate": "0001-01-01T00:00:00",
        "ReadDate": null,
        "Id": 3
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "blocked test",
        "SentDate": "0001-01-01T00:00:00",
        "ReadDate": null,
        "Id": 4
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "blocked other way test",
        "SentDate": "0001-01-01T00:00:00",
        "ReadDate": null,
        "Id": 5
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "date test",
        "SentDate": "2020-04-11T00:00:00",
        "ReadDate": null,
        "Id": 6
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "date test",
        "SentDate": "2020-04-11T00:00:00",
        "ReadDate": null,
        "Id": 7
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "id test",
        "SentDate": "0001-01-01T00:00:00",
        "ReadDate": null,
        "Id": 8
    },
    {
        "From": "0617c281-5488-4009-bf37-761f6cfea2df",
        "To": "f54b7bf4-c7b5-482c-83b8-5b61e78ea398",
        "Text": "id test",
        "SentDate": "0001-01-01T00:00:00",
        "ReadDate": null,
        "Id": 9
    }
]

My code is the following:
public IEnumerable<AspNetUserMessage> GetAllMessages([FromBody]MessageRequest Request)
        {
            try
            {
                // Get all the messages sent or received by the user
                var messages = db.AspNetUserMessages.Where(o => o.From == Request.From || o.To == Request.From).ToList();

                return messages;
            }
            catch (Exception e)
            {
                // Log the error to the database
                LogException(e);
                return null;
            }
        }

What I want to do is instead of returning all the messages, I want to return a categorized version of the messages. E.g
User1 sent 4 messages to User2;
User1 sent 2 messages to User3
For user1 output should be 
[
{
  "SentTo": "User2"
  "Messages": [All 4 Messages sent to User2 here]
},
{
  "SentTo": "User3"
  "Messages": [All 2 Messages sent to User3 here]
}
]

How can I achieve this modifying my GetAllMessages function

A:

You have to make a a new model that uses your AspNetUserMessage and fill it.

Make a new model, for now I named it SentMessagesModel

public class SentMessagesModel{
  public string SentTo {get;set;}
  public List<AspNetUserMessage> Messages {get;set;}
  // All 4 Messages sent to User2 here
}

Then make a new controller action that uses the new model. Use the code below

public List<SentMessagesModel> GetAllMessages([FromBody]MessageRequest Request)
{
   try
   {
      // Get all the messages sent or received by the user
      var messages = db.AspNetUserMessages.Where(o => o.From == Request.From).ToList();
      var groupByTo = messages.GroupBy(m=>m.To);

      List<SentMessagesModel> sentMessages = new List<SentMessagesModel>();

      foreach(var currentGroup in groupByTo){
         sentMessages.Add(new SentMessagesModel(){ SentTo = currentGroup.Key, Messages = currentGroup.ToList() })
      }

      return sentMessages;
   }
   catch (Exception e)
   {
      // Log the error to the database
      LogException(e);
      return null;
   }
}

