C
C#9mo ago
ItaM

❔ SOLVER Ordering messages based on the receiver or sender last message

I have this piece of code
public async Task<List<UserLimitedDto>> GetUsersThatHaveMessagedMe()
{
var (_, _, username) = await _tokenService.DecodeHS512Token();

var users = MessageJoinQuery()
.Where(u => u.Receiver_Username == username)
.GroupBy(u => u.Sender)
.Select(group => group.OrderByDescending(m => m.Timestamp).FirstOrDefault())
.ToList();

return users
.OrderByDescending(m => m.Timestamp)
.Select(u => new UserLimitedDto
{
User_Id = u.Sender_User_Id,
Username = u.Sender_Username,
Profile_Picture = u.Sender_Profile_Picture
})
.ToList();
}
public async Task<List<UserLimitedDto>> GetUsersThatHaveMessagedMe()
{
var (_, _, username) = await _tokenService.DecodeHS512Token();

var users = MessageJoinQuery()
.Where(u => u.Receiver_Username == username)
.GroupBy(u => u.Sender)
.Select(group => group.OrderByDescending(m => m.Timestamp).FirstOrDefault())
.ToList();

return users
.OrderByDescending(m => m.Timestamp)
.Select(u => new UserLimitedDto
{
User_Id = u.Sender_User_Id,
Username = u.Sender_Username,
Profile_Picture = u.Sender_Profile_Picture
})
.ToList();
}
What this does its that it gets a list of Users which meet a certain criteria. I have managed to order them based on the last message that they sent me, and it works pretty well. However, I am unable to order them based on the last message that I (receiver) sent them. It wont get updated
16 Replies
ItaM
ItaM9mo ago
This is an example of the functionality so far, as you can see. When another user (sender) sends a message, their icon goes up. As you can see, at the beggining it all works fine, new messages from sender, their profiles go up. But when the receiver sends a message, it does not make the icon go up I need to order the users based on the last message of either the sender or receiver, and currently its only getting ordered based on the senders last message
ItaM
ItaM9mo ago
I suspect the issue might be inside this select
ItaM
ItaM9mo ago
For reference, this is the MessageJoinQuery
WEIRD FLEX
WEIRD FLEX9mo ago
is this a practice/test application? because to me looks like this queries when you have upwards of thousands messages would probably slow down a bit also it's a bit unclear to me what's happening in the video why does the left window have the icons and the right one doesn't? is it a room?
ItaM
ItaM9mo ago
kind of a practice application im creating a table called messages where i pretty much store all the messages, which you are right, might store thousands and thousands of messages which might be slow
ItaM
ItaM9mo ago
This is pretty much it
ItaM
ItaM9mo ago
the left window has icons because its the actual users chat, in their profile. similar to facebook messenger chat, the second one does not have icons because its just another user visiting someone elses profile and opening a chat and sending them a message
WEIRD FLEX
WEIRD FLEX9mo ago
ok, so next the .Where(u => u.Receiver_Username == username) kinda seems limiting your query based on the logic you described if that's the method that does all the work and there aren't other queries
ItaM
ItaM9mo ago
yep, something i tried was doing the .Where like this .Where(m => m.Receiver_Username == username || m.Sender_Username == username) This selected all the messages of a certain conversation, pretty much. Then grouping them like this .GroupBy(m => new { m.Sender_Username, m.Receiver_Username }) And this ordered them almost the way I wanted to, however, the result was having the sender icons in the final result
ItaM
ItaM9mo ago
So I kinda wanted to do this, get sender and receiver messages, group them together, order them based on the timestamp of the message, and then remove the sender messages. But this code didnt return anything
ItaM
ItaM9mo ago
Do you know if there is a way to add a breakpoint on every like line of code on that link query without having to materialize the query/ save it into memory?
WEIRD FLEX
WEIRD FLEX9mo ago
put breakpoints and debug like this would take a lot of time because it would make you step through every item what i would do is separate the query sort of for every line like
var users = MessageJoinQuery();
var users_where = users.Where(_ => _.Receiver_Username == ...);
var users_select = users_where.Select(group => ...);
...
var users = MessageJoinQuery();
var users_where = users.Where(_ => _.Receiver_Username == ...);
var users_select = users_where.Select(group => ...);
...
and then look at the result for every operation if it corresponds to what i think is happening using the table view it shouldn't be a long process
ItaM
ItaM9mo ago
that pretty much saves it into memory, its a good way. Ill do it like that Btw, I fixed it
ItaM
ItaM9mo ago
ItaM
ItaM9mo ago
This was the final result
Accord
Accord9mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ (Solved) (Dapper) how to make a single query with one-to-many objects?``` connection.QueryAsync<Request, Patient, Registrant, Request>``` i don't think `QuerySingleAsync`✅ WPF app keeps launching Console WindowHey all, new here, I believe this is the correct forum to ask this. Anyways, I am building a wpf app❔ Suggest a good ASP.NET Core MVC Tutorial that can help me get Started.Hello, I finished with the fundamental C# with FreeCodeCamp and Microsoft on September 9, 2023. I wo❔ Cant merge the same audio file twice or more using WaveFileWriter.CreateWaveFile16();Hello, Im trying to merge the audio files using **NAudio**, but if they repeat it doesnt cout it. ✅ What is the best way to store basic C#?Hi, I'm testing my C# code and learning from it. For example deserializing and serializing JSON usin❔ Question about Layered StructureGuys do you think it is bad to expose Entities Layer both DAL and Services layer? And where should I❔ ✅ Swagger not sending Guid string correctlyHello all, I have a controller set up exactly the same as some other controllers to get an item by a✅ Web things minimal apiafter tons of codding i totally forgot what i wanted to do lmao, so problem is - product has click e❔ Cast is not valid when casting interface to comcrete implthis is my concrete imp: public class TempFile : IBrowserFile { public TempFile(string u✅ finding dotnet 8.0.0-rc.1.23419.4 ?Hi. I'm trying to get dotnet-ef working with dotnet preview 8.0 It seems that it supports rc 1.23419