在mvc5中为Join Query提供View-model的替代方法是什么

时间:2015-02-16 05:33:43

标签: linq asp.net-mvc-4 asp.net-mvc-5

我使用View模型在UserViewVieModel'中传递不同的模型。 我的控制器的语法是

 select new UserInfoViewModel
             {
               UserInfoID = user.UserInfoID,
               AvatarURL = user.AvatarURL,
               UserFullName = user.UserFullName,
               ThreadSnippet = c.ThreadSnippet,
               MessageStarterUserId = c.MessageStarterUserId,
               MultipleReceiversId = c.MultipleReceiversId,
               LastMessageTime = c.LastMessageTime
              }

我的关注点是,当我通过这个模型时,我得到了我在UserinfoVieModel中描述的所有类的所有空白数据

public class UserInfoViewModel
    {
        public virtual string Id { get; set; }
        public Guid UserInfoID { get; set; }
        public string UserName { get; set; }
        public string UserID { get; set; } 
        public String DbType { get; set; }
        public Guid UserLevelEnumId { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public DateTime Birthdate { get; set; }
        public string Gender { get; set; }//male/female/unknown
        public string Email { get; set; }
        public string MobileNo { get; set; }
        public string Country { get; set; }
        public int Zipcode { get; set; }
        public string Caption { get; set; }
        public int CountFollowers { get; set; }
        public int CountFollows { get; set; }
        public int CountFiles { get; set; }
        public int CountPhotos { get; set; }

\\ and many other Attributes
}

所以请告诉我一些方法,以便我的linq查询性能通过接收我想要的特定数据而增加。

这是我的行动方法

 var searchMessageThread = from c in db.MessageThreads 
                                      where c.MessageStarterUserId == loginUserId && c.isGroup == false
                                      join user in db.UserInfos
                                      on c.MultipleReceiversId equals user.UserInfoID
                                      select new UserInfoViewModel
                                      {
                                          UserInfoID = user.UserInfoID,
                                          AvatarURL = user.AvatarURL,
                                          UserFullName = user.UserFullName,
                                          ThreadSnippet = c.ThreadSnippet,
                                          MessageStarterUserId = c.MessageStarterUserId,
                                          MultipleReceiversId = c.MultipleReceiversId,
                                          LastMessageTime = c.LastMessageTime
                                      }
                                      ;
            return PartialView("_GetThread", searchMessageThread.OrderByDescending(s => s.LastMessageTime).ToList());

1 个答案:

答案 0 :(得分:1)

如果您不想绑定view_model,请更改您的操作方法,如下所示

var searchMessageThread = from c in db.MessageThreads 
                                  where c.MessageStarterUserId == loginUserId && c.isGroup == false
                                  join user in db.UserInfos
                                  on c.MultipleReceiversId equals user.UserInfoID
                                  select new 
                                  {
                                      UserInfoID = user.UserInfoID,
                                      AvatarURL = user.AvatarURL,
                                      UserFullName = user.UserFullName,
                                      ThreadSnippet = c.ThreadSnippet,
                                      MessageStarterUserId = c.MessageStarterUserId,
                                      MultipleReceiversId = c.MultipleReceiversId,
                                      LastMessageTime = c.LastMessageTime
                                  }
                                  ;
        return PartialView("_GetThread", searchMessageThread.OrderByDescending(s => s.LastMessageTime).ToList());

选中新的