LINQ to Entities - Selezionare tutti gli amici e la Chat tra di loro

0

Domanda

Come Selezionare tutti gli amici dell'utente attualmente connesso e la Chat Privata Chat (Id) tra l'utente e i loro amici? Io sono in grado di ottenere gli amici, ma sto avendo difficoltà a ottenere anche la Chat tra di loro.

            // Select all the User's friends and the chat (Id) between them
            var friends = await _context.Friendships // Get the Friendships
                .Include(x => x.Friend).ThenInclude(x => x.ChatUsers).ThenInclude(x => x.Chat)
                .Where(x => x.Status == StatusCode.Accepted && x.ApplicationUserId == userId)
                .Select(x => x.Friend)
                .ToListAsync();

Tavolo dell'amicizia

    public class Friendship
    {
        // The primary keys/foreign keys of the associated tables
        public string ApplicationUserId { get; set; }
        public string ApplicationFriendUserId { get; set; }

        // Reference to the user that has the friend
        public User ApplicationUser { get; set; }

        // Reference to the friend
        public User Friend { get; set; }

        // The status of the friendship
        public StatusCode Status { get; set; }
    }

Tabella utente

    public class User : IdentityUser
    {
        // Reference to all user's chats
        public ICollection<ChatUser> ChatUsers { get; set; }

        // Reference to all the user's friendships
        public ICollection<Friendship> UsersFriendships { get; set; }

        // Reference to all the friend's friendships (their point of view)
        public ICollection<Friendship> FriendsFriendships { get; set; }
    }

ChatUser tabella

    public class ChatUser
    {
        // The primary key/foreign keys of the associated tables
        public int ChatId { get; set; }
        public string UserId { get; set; }

        // The role that the User can be
        public UserRole Role { get; set; }

        // Reference to the chat
        public Chat Chat { get; set; }

        // Reference to the user
        public User User { get; set; }
    }

Chat

    
    public class Chat
    {
        // The primary key
        public int Id { get; set; }

        // The chat's name
        public string Name { get; set; }

        // The chat type, e.g room, private
        public ChatType Type { get; set; }

        // Reference to all the Chat's Users
        public ICollection<ChatUser> ChatUsers { get; set; }
    }

Grazie

1

Migliore risposta

1

Questa query:

var friends = await _context.Friendships // Get the Friendships
    .Include(x => x.Friend).ThenInclude(x => x.ChatUsers).ThenInclude(x => x.Chat)
    .Where(x => x.Status == StatusCode.Accepted && x.ApplicationUserId == userId)
    .Select(x => x.Friend)
    .ToListAsync();

... carichi l'utente amici con gli amici corrispondente chat che comprendono chat non con l'utente corrente.

Con la struttura di dominio per le chat e le amicizie, sembra piuttosto difficile da provare e collegarli in modo significativo. È probabile possibile con un semplice approccio a due passaggi:

var friendIds = _context.Users
    .Where(x => s.UserId == userId)
    .SelectMany(x => x.UsersFriendships.Where(f => f.Status == StatusCode.Accepted).Select(f => f.ApplicationFriendUserId))
    .ToList(); // Get all accepted Friend IDs.


 var chats = _context.Chats
     .Where(x => x.ChatUsers.Any(cu => cu.UserId) && x => x.ChatUsers.Any(cu => friendIds.Contains(cu.UserId)
     .Select(x => new 
     {
         Chat = x,
         Friends = x.ChatUsers.Where(cu => friendIds.Contains(cu.UserId)).Select(cu => cu.User).ToList()
      }).ToList();

Le chat sono relativi a due o più utenti, e non sono limitati/legato alle amicizie.

Joe avrebbe potuto essere amici con Sam, Jane e John e di avere il seguente chat attiva:

Chat 1: Joe <-> Sam

Chat 2: Joe <-> Jane

Chat 3: Joe <-> Elena <-> Sam

Chat 4: Joe <-> Frank

Chat 5: Joe <-> Frank <-> Sam

Vorremmo che la Chat 1, 2, 3, e 5 restituito. Non ci sono chat con John, e non ci si cura di chat con Frank, ma non si preoccupano l'uno con Frank & Sam Sam è un amico. L'obiettivo sarebbe quello di identificare le chat che Joe partecipa con uno o più dei suoi amici. Per ogni partita si ritorna la chat e tutti gli Amici attualmente anche in chat.

L'avvertenza di due-pass approccio è che esso presuppone che la lista di amici vorresti rimanere ragionevolmente piccolo, non è sufficiente a superare la IN() elenco che sarebbe generato per ottenere la corrispondenza di Chat.

2021-11-23 04:50:49

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................