LINQ to Entități - Selectați toate Utilizator prieteni și Chat-ul între ele

0

Problema

Cum pot Selecta toți prietenii de utilizatorul conectat curent și Private Chat (Chat Id) între utilizator și prietenii lor? Sunt capabil de a obține prietenii utilizatorului, dar am probleme, de asemenea, obtinerea Chat-ul între ele.

            // 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();

Prietenia masa

    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; }
    }

Masa de utilizator

    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 masa

    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; }
    }

Multumesc

1

Cel mai bun răspuns

1

Această interogare:

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();

... sarcini utilizatorul prieteni cu prietenii pe chat-uri corespunzătoare care să includă discuții nu cu utilizatorul curent.

Cu acel domeniu de structură pentru chat-uri și prieteniile se pare destul de dificil pentru a încerca și de a le lega într-un mod semnificativ. Este probabil, posibil, cu un simplu două-pass abordare:

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();

Chat-urile sunt legate la două sau mai mulți utilizatori, și nu sunt restricționate/legat o prietenie.

Joe i-ar putea fi prieten cu Sam, Jane, și Ioan și au următoarele chat-uri active:

Chat 1: Joe <-> Sam

Chat 2: Joe <-> Jane

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

Chat 4: Joe <-> Frank

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

Am vrea Chat-urile 1, 2, 3, și 5 revenit. Nu sunt discuții cu John, și nu ne pasă de chat cu Frank, dar nu le pasă cel cu Frank & Sam când Sam este un prieten. Scopul ar fi de a identifica care chat-uri pe care Joe participă la una sau mai multe dintre prietenii lui. Pentru fiecare meci vom reveni chat și Prieteni, în prezent, de asemenea, în discuție.

Avertisment de două-pass abordări este că se presupune că lista de prieteni va rămâne rezonabil de mici, nu suficient de mare pentru a depăși IN() lista care va fi generat pentru a obține de potrivire Chat-uri.

2021-11-23 04:50:49

În alte limbi

Această pagină este în alte limbi

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