Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:57:36+00:00 2026-06-17T12:57:36+00:00

I am coding a message program something like WhatsApp. I want to show the

  • 0

I am coding a message program something like WhatsApp.

I want to show the last message between user and his/her friend. But to do this, I am getting whole messages between them and using some query to find the last message. I think that I can re-write a better way. Because with that way, I get all message to memory. If I have a million messages, thats not a good way

    var myID = Convert.ToInt32(txtID.Text);
    var myMessages = db.MemberMessages
        .Where(m => m.MemberTo == myID || m.MemberFrom == myID)
        .Select(s => new
            {
                s.ID,
                s.Text,
                s.MemberFrom,
                s.MemberTo
            });

    List<int> messageIDs = myMessages
        .Where(s => s.MemberTo == myID).Select(s => s.MemberFrom).Distinct().ToList();

    List<int> messageIDs2 = myMessages
        .Where(s => s.MemberFrom == myID)
        .Select(s => s.MemberTo).Distinct().ToList();

    List<int> differentIDS = messageIDs2.Except(messageIDs).ToList();

    foreach (int item in differentIDS)
    {
        messageIDs.Add(item);
    }

    foreach (int userID in messageIDs)
    {
        var deger = myMessages
            .Where(m => 
                (m.MemberFrom == myID && m.MemberTo == userID) ||
                (m.MemberFrom == userID && m.MemberTo == myID))
                .OrderByDescending(m => m.ID).ThenByDescending(m => m.MemberFrom).First();

        if (deger.MemberFrom == myID)
            lstLastMessages.Items.Add(String.Format("From Me - To ID:{0}  -  Message: {1}", deger.MemberTo, deger.Text));
        else
            lstLastMessages.Items.Add(String.Format("From ID: {0} - To: Me -Message: {1}", deger.MemberFrom, deger.Text));
    }

You can see the last message between the user and his/her friends who have different ID. Is it possible to re-write it with one query? Or a shorter way?

Looking for some advice.

edit:
enter image description here

As you see my table, I have some record among users. For example, my ID is 1 and I want to find the last messages that I talked users before.

memberfrom 1 (its me)  memberfromTo : 2 . message: xxx 
memberfrom 2 (its me)  memberfromTo : 1 . message: yyy
memberfrom 1 (its me)  memberfromTo : 2 . message: zzzz

If you look above, I want to get the last message is zzzz. But I want to do it for each users who sent the last message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:57:38+00:00Added an answer on June 17, 2026 at 12:57 pm

    Just group messages by MemberTo and MemberFrom fields, and get last message from each group ordered by CreationTime:

    var query = from m in db.MemberMessages
                where m.MemberTo == id || m.MemberFrom == id
                group m by new { m.MemberTo, m.MemberFrom } into g
                select g.OrderByDescending(x => x.CreationTime).First();
    

    This will return both last message from user and to user you are looking for (because groups will have different keys).

    UPDATE

    var query = from m in db.MemberMessages
                let msgTo = m.MemberTo == id
                let msgFrom = m.MemberFrom == id 
                where msgTo || msgFrom
                group m by msgTo ? m.MemberFrom : m.MemberTo into g
                select g.OrderByDescending(x => x.CreationTime).First();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is weird question but I want to know how to change message coming
Have tried to compile and run this program, but have received this error message
I am coding a message distribution system. Server is ColdFusion (CF) 10, using the
I am coding for a Netty based Notifying Server, which takes in Message Buffer
I'm seeing this exception message coming from XslCompiledTransform.Transform(), but after handling the exception the
So I am coding this client/server program. This code is from the client side.
Please help me with a spellcheck program in C. The majority of the coding
I have an existing program where a message (for example, an email, or some
Hi I have a small problem. I am coding a program that displays the
I was coding on my project and wanted to debug it, but I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.