LINQ to Entity - Vyberte všechny Uživatele přátel a Chat, mezi nimi

0

Otázka

Jak mohu Vybrat všechny přátele aktuálně přihlášeného uživatele a Soukromé Rozhovory (Chat Id) mezi uživateli a jejich přáteli? Jsem schopen se dostat na uživatele přátel, ale mám problém se i dostat na Chat mezi 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();

Přátelství stůl

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

Uživatelská tabulka

    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 stůl

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

Děkuji

1

Nejlepší odpověď

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

... načte uživatelská přátelé s přáteli odpovídající chaty, která by měla zahrnovat povídání není s aktuální uživatel.

S tím doménové struktury pro rozhovory a přátelství, to vypadá spíše složité zkuste a odkaz je v smysluplným způsobem. Je pravděpodobné, že je to možné s jednoduchým dvou-pass pří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();

Chaty jsou spojeny dva nebo více uživatelů, a nejsou omezeny/spojené s přátelství.

Joe by mohl být přátelé s Sam, Jane a John a mají následující chaty aktivní:

Chat 1: Joe <-> Sam

Chat 2: Joe <-> Jane

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

Chat 4: Joe <-> Frank

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

Chtěli bychom Chaty 1, 2, 3, a 5 se vrátil. Nejsou žádné rozhovory s Johnem, a nestaráme se o chatu s Frankem, ale záleží s Frank & Sam od Sam je můj přítel. Cílem by bylo určit, které z chaty, že Joe se účastní jeden nebo více z jeho přátel. Pro každý zápas jsme se vrátit na chatu a Přátele v současné době také v tom chatu.

Upozorněním, dvou-pass přístupu je, že předpokládá, že seznam přátel zůstane poměrně malý, není dost velký na to, aby nepřekročil IN() seznam, který bude generován získat odpovídající Chaty.

2021-11-23 04:50:49

V jiných jazycích

Tato stránka je v jiných jazycích

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