Varlıklara Bağlantı - Kullanıcının tüm arkadaşlarını ve aralarındaki Sohbeti seçin

0

Soru

Oturum açmış kullanıcının tüm arkadaşlarını ve kullanıcı ile arkadaşları arasındaki Özel Sohbetleri (Sohbet Kimliği) nasıl seçebilirim? Kullanıcının arkadaşlarını alabiliyorum ama aralarındaki Sohbeti de almakta zorlanıyorum.

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

Dostluk masası

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

Kullanıcı tablosu

    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 tablosu

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

Sohbet

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

Teşekkür ederim

1

En iyi cevabı

1

Bu sorgu:

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

... kullanıcı arkadaşlarını, geçerli kullanıcıyla olmayan sohbetleri içeren arkadaşlara karşılık gelen sohbetlerle yükler.

Sohbetler ve arkadaşlıklar için bu alan yapısıyla, onları anlamlı bir şekilde bağlamaya çalışmak oldukça zor görünüyor. Muhtemelen basit bir iki geçişli yaklaşımla mümkündür:

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

Sohbetler iki veya daha fazla kullanıcıyla ilgilidir ve arkadaşlıklarla sınırlı / bağlantılı değildir.

Joe, Sam, Jane ve John ile arkadaş olabilir ve aşağıdaki sohbetleri aktif hale getirebilir:

Sohbet 1: Joe < - > Sam

Sohbet 2: Joe < - > Jane

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

Sohbet 4: Joe < - > Frank

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

1, 2, 3 ve 5 numaralı Sohbetlerin geri dönmesini istiyoruz. John'la sohbet yok ve Frank'le olan sohbeti umursamıyoruz, ama Sam bir arkadaş olduğu için Frank ve Sam'le olanı önemsiyoruz. Amaç, Joe'nun bir veya daha fazla arkadaşıyla hangi sohbetlere katıldığını belirlemek olacaktır. Her maç için sohbeti ve şu anda o sohbette bulunan tüm Arkadaşları iade ediyoruz.

Arkadaş listesi oldukça küçük kalır aşmak için yeterince büyük olacağını varsayar iki taramalı yaklaşım uyarı yok IN() eşleşen Sohbetleri almak için oluşturulacak liste.

2021-11-23 04:50:49

Diğer dillerde

Bu sayfa diğer dillerde

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