public IQueryable <ContactInfo> MyContacts(string userId) { return(Conversations .AsNoTracking() .Where(t => !(t is PrivateConversation) || ((PrivateConversation)t).RequesterId == userId || ((PrivateConversation)t).TargetId == userId) .Where(t => !(t is GroupConversation) || ((GroupConversation)t).Users.Any(p => p.UserId == userId)) .Select(t => new ContactInfo { ConversationId = t.Id, Discriminator = t.Discriminator, DisplayName = (t is PrivateConversation) ? (userId == ((PrivateConversation)t).RequesterId ? ((PrivateConversation)t).TargetUser.NickName : ((PrivateConversation)t).RequestUser.NickName) : ((GroupConversation)t).GroupName, DisplayImagePath = (t is PrivateConversation) ? (userId == ((PrivateConversation)t).RequesterId ? ((PrivateConversation)t).TargetUser.IconFilePath : ((PrivateConversation)t).RequestUser.IconFilePath) : ((GroupConversation)t).GroupImagePath, UserId = (t is PrivateConversation) ? (userId == ((PrivateConversation)t).RequesterId ? ((PrivateConversation)t).TargetId : ((PrivateConversation)t).RequesterId) : ((GroupConversation)t).OwnerId, UnReadAmount = (t is GroupConversation) ? t.Messages.Count(m => m.SendTime > ((GroupConversation)t).Users.SingleOrDefault(u => u.UserId == userId).ReadTimeStamp) : t.Messages.Count(p => !p.Read && p.SenderId != userId), LatestMessage = t.Messages.OrderByDescending(p => p.SendTime).Select(m => m.Content).FirstOrDefault(), LatestMessageTime = t.Messages.Max(m => m.SendTime), Muted = (t is GroupConversation) ? ((GroupConversation)t).Users.SingleOrDefault(u => u.UserId == userId).Muted : false, AesKey = t.AESKey, SomeoneAtMe = (t is GroupConversation) ? t.Messages .Where(m => m.SendTime > ((GroupConversation)t).Users.SingleOrDefault(u => u.UserId == userId).ReadTimeStamp) .Any(p => p.Ats.Any(k => k.TargetUserId == userId)) : false }) .OrderByDescending(t => t.SomeoneAtMe) .ThenByDescending(t => t.LatestMessageTime)); }