LINQ Subjektom - Vyberte všetky Používateľa priateľov a Chat medzi nimi

0

Otázka

Ako môžem si Vybrať všetkých priateľov, aktuálne prihlásený používateľ a Súkromné Rozhovory (Chat Id) medzi používateľom a ich priateľov? Som schopný sa dostať užívateľa priateľov, ale mám problémy s tiež dostať Rozhovor medzi nimi.

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

Priateľstvo tabuľka

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

Používateľ tabuľka

    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 tabuľka

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

Ďakujeme

1

Najlepšiu odpoveď

1

Tento dotaz:

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

... zaťaženie užívateľ priateľov s priateľmi zodpovedajúce chaty, ktorá bude obsahovať rozhovory s aktuálneho používateľa.

S štruktúry domény pre chaty a priateľstvá vyzerá to dosť zložité a skúste a ich prepojenie zmysluplným spôsobom. Je pravdepodobné, možné jednoduché dva-pass prístup:

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

Rozhovory sa týkajú dvoch alebo viacerých používateľov, a nie sú obmedzené/spojené s priateľstvá.

Joe by mohli byť priatelia s Sam, Jane, a Jána a majú tieto chaty aktívne:

Chat 1: Joe <-> Sam

Chat 2: Joe <-> Jane

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

Rozhovor 4: Joe <-> Frank

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

Budeme chcieť Chaty 1, 2, 3, a 5, vrátený. Nie sú žiadne rozhovory s Jánom a nezaujíma rozhovor s Frankom, ale sa starajú o jednej, Frank & Sam od Sam je priateľ. Cieľom bude identifikovať, ktoré diskusie, ktoré Joe sa zúčastňuje jeden alebo viac z jeho priateľov. Na každý zápas sme sa vrátiť na chatu a Priateľov a v súčasnosti aj v tom, že chat.

Námietka dvoch-pass prístupu je, že predpokladá sa, že priateľ zoznam by zostať primerane malé, nie je dostatočne veľký, aby neprekračoval IN() zoznam, ktorý by generované získať zodpovedajúce Chaty.

2021-11-23 04:50:49

V iných jazykoch

Táto stránka je v iných jazykoch

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