本文整理汇总了C#中Data.List.ToDictionary方法的典型用法代码示例。如果您正苦于以下问题:C# List.ToDictionary方法的具体用法?C# List.ToDictionary怎么用?C# List.ToDictionary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Data.List
的用法示例。
在下文中一共展示了List.ToDictionary方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SelectFriends
public ActionResult SelectFriends(string searchTerm)
{
var loggedUserId = this.User.Identity.GetUserId();
// Take friend relaions where friend's name contains searchTerm
var friendRelations = this.Data
.FriendRelations
.All()
.Project()
.To<FriendRelationViewModel>()
.Where(fr =>
((fr.FirstUser.Id == loggedUserId && fr.SecondUser.UserName.ToLower().Contains(searchTerm.ToLower())) ||
(fr.FirstUser.UserName.ToLower().Contains(searchTerm.ToLower()) && fr.SecondUser.Id == loggedUserId)) &&
(fr.AreFriends == true))
.ToList();
if (friendRelations.Any())
{
// Extract friends users from friend relations
var users = new List<UserViewModel>();
foreach (var friendRelation in friendRelations)
{
users.Add(friendRelation.FirstUser.Id == loggedUserId
? friendRelation.SecondUser
: friendRelation.FirstUser);
}
var chats = new List<ChatViewModel>();
foreach (var user in users)
{
// Check if two users had already chat, if not create new chat
var chat = this.Data.Chats.All().Project().To<ChatViewModel>().FirstOrDefault(ch =>
(ch.FirstUser.Id == user.Id && ch.SecondUser.Id == loggedUserId) ||
(ch.FirstUser.Id == loggedUserId && ch.SecondUser.Id == user.Id));
if (chat == null)
{
var newChat = new Chat()
{
FirstUserId = loggedUserId,
SecondUserId = user.Id,
Messages = new List<Message>()
};
this.Data.Chats.Add(newChat);
this.Data.SaveChanges();
var newChatToView =
this.Data.Chats.All()
.Project()
.To<ChatViewModel>()
.FirstOrDefault(ch => ch.FirstUser.Id == loggedUserId && ch.SecondUser.Id == user.Id);
chats.Add(newChatToView);
}
else
{
chats.Add(chat);
}
}
var returnFriendsMessages = chats
.ToDictionary(chat =>
chat.FirstUser.Id == loggedUserId ? chat.SecondUser : chat.FirstUser,
chat => chat.Messages.Any() ? chat.Messages.Last() : null);
return PartialView("_DisplayFriendsMessages", returnFriendsMessages);
}
return new EmptyResult();
}