LINQ 엔터티-모두를 선택하는 사용자의 친구와 채팅 그들 사이의

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

...드 사용자 친구는 친구들과 채팅 해당하는 채팅을 포함하지 않으로 현재 사용자.

으로하는 도메인 구조를 채팅에 대한 우정을 보이는 오히려 까다로운 시도와 링크에서 그 의미 있는 방법입니다. 그것은 가능성이 가능한 패스 접근 방식:

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

채팅 내용은 관련하여 두 개 이상의 사용자,그리고 그들은 제한되지 않/링크 우정을 쌓아가세요.

조 친구가 될 수 Sam,제인,요한은 다음과 같은 채팅을 활성:

1:Joe<->Sam

채 2:Joe<->인

담 3:Joe<->인<->Sam

채팅을 4:Joe<->솔직

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

우리가 원하는 채팅을 1,2,3,5 이 반환됩니다. 기와 채팅,그리고 우리는 걱정하지 않은 채팅 프랭크하지만,관리에 대해 하나의 프랭크와 함께&Sam 샘 친구입니다. 하는 것이 목표를 식별하는 채팅을 조에 참여하는 하나 이상의 자신의 친구들. 각 경기에 대한 우리는 돌아 채팅을하고 모든 친구들이 현재 또는 채팅.

경고의 전달 방법은 있는 그는 친구 목록에 남아 있을 것입 합리적으로 작은,충분히 큰되지 않을 초과하는 IN() 목록 생성되는하여 일치하는 채팅.

2021-11-23 04:50:49

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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