LINQ จะ Entities เลือกทั้งหมดของผู้ใช้เพื่อนกันและคุยกันระหว่างพวกเขา

0

คำถาม

จะให้ฉันเลือกทั้งหมดเป็นเพื่อนของอยู่ในปัจจุบันอกอินของผู้ใช้และการคุยส่วนตัว(คุยกัน Id)ระหว่างผู้ใช้และพวกเขาเป็นเพื่อน? ฉันสามารถที่จะให้ผู้ใช้เป็นเพื่อนกันแต่ฉันกำลังมีปัญหาก็ได้คุยกันระหว่างพวกเขา

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

มิตรภาพบนโต๊ะ

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

โต๊ะของผู้ใช้

    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 โต๊ะ

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

คุยกัน

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

ขอบคุณ

1

คำตอบที่ดีที่สุด

1

นี้ไปในไดเรคทอรี:

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

...ความของผู้ใช้เพื่อนกับเพื่อนที่สัมพันธ์กันการคุยซึ่งจะรวมถึงการคุยไม่ได้อยู่กับโดยผู้ใช้ปัจจุบัน

กับโดเมนโครงสร้างของสำหรับการคุยกั friendships มันดูดีกว่าเรื่องซับซ้อนพยายามและเชื่อมโยงพวกเขาในทางที่มีประโยชน์บ้าง. มันก็ดูเหมือนจะเป็นไปได้ด้วยของธรรมดาแบบสองคน-ขอผ่านเข้ามา:

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

การคุยเป็นเกี่ยวข้องกับสองหรือมากกว่าผู้ใช้และพวกเขาไม่ได้ถูกห้าม/เกี่ยวข้องกับมิตรภาพ

โจอาจจะเป็นเพื่อนกับแซม,เจนและจอห์นและมีคนติดตามการคุยที่ทำงานอยู่:

คุยกัน 1:โจ<->แซม

คุยกัน 2:โจ<->เจน

คุยกัน 3:โจ<->เจน<->แซม

คุยกัน 4:โจ<->แฟรงค์

คุยกัน 5:โจ<->แฟรงค์<->แซม

เราต้องการการคุย 1,2,3,และอีก 5 กลับมาแล้ว ไม่มีการคุยกับจอห์น,และพวกเราไม่สนเรื่องคุยกับแฟรงค์แต่สนใจเรื่องหนึ่งเดียวกับแฟรงค์&ตั้งแต่แซมแซมเป็นเพื่อน เป้าหมายจะต้องระบุตัวซึ่งการคุยนั่นโจคือ..มีส่วนร่วมอยู่กับใครหรือมากกว่าเพื่อนของเขานะ เพื่อกันและกันเราจะตอบแทนคุยกันและมีเพื่อนอยู่ในปัจจุบันยังอยู่ในนั้นคุยกัน

ที่ caveat ของสองคน-ขอผ่านเข้าใกล้มันมักจะเดาว่ามันคเพื่อนของรายการก็จะยังคง reasonably เล็กไม่ใหญ่พอที่จะเกินค IN() รายการนั่นคงเป็นที่ถูกสร้างให้คนจับคู่เธอเลือกใช่ไหม

2021-11-23 04:50:49

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่