LINQ संस्थाओं के लिए - का चयन करें सभी उपयोगकर्ता के दोस्तों और चैट के बीच उन्हें

0

सवाल

मैं कैसे कर सकते हैं का चयन सभी दोस्तों के वर्तमान में उपयोगकर्ता में लॉग इन और निजी चैट (चैट आईडी के रूप में) उपयोगकर्ता के बीच और उनके दोस्तों? मैं कर रहा हूँ प्राप्त करने में सक्षम उपयोगकर्ता के दोस्तों, लेकिन मैं कर रहा हूँ परेशानी भी हो रही है, चैट, उन दोनों के बीच.

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

चैट करने के लिए संबंधित हैं दो या दो से अधिक उपयोगकर्ताओं, और वे प्रतिबंधित नहीं कर रहे हैं/दोस्ती करने के लिए जुड़ा हुआ.

जो हो सकता है दोस्तों के साथ सैम, जेन, और जॉन और निम्न चैट सक्रिय:

चैट 1: जो <-> सैम

चैट 2: Joe <-> जेन

चैट 3: जो <-> जेन <-> सैम

चैट 4: Joe <-> फ्रैंक

चैट 5: Joe <-> फ्रैंक <-> सैम

हम चाहते हैं चैट 1, 2, 3, और 5 लौटे. वहाँ कोई नहीं कर रहे हैं के साथ चैट जॉन, और हम परवाह नहीं है के बारे में चैट फ्रैंक के साथ, लेकिन के बारे में परवाह नहीं के साथ एक फ्रैंक और सैम के बाद से सैम का एक दोस्त है. लक्ष्य के लिए किया जाएगा की पहचान करने के लिए जो कि चैट के लिए जो में भाग लेने के एक या अधिक के साथ अपने दोस्तों को भेजें । प्रत्येक मैच के लिए हम वापसी चैट और किसी भी दोस्तों वर्तमान में भी उस चैट.

चेतावनी के दो-पास दृष्टिकोण यह है कि यह मानता है कि दोस्त की सूची रहेगा यथोचित छोटे, नहीं बड़ा करने के लिए पर्याप्त से अधिक है IN() सूची उत्पन्न हो जाएगा कि प्राप्त करने के लिए मिलान चैट.

2021-11-23 04:50:49

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

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

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में