开发者

How to set joined table result into Model Class

开发者 https://www.devze.com 2023-03-24 18:33 出处:网络
I have two tables like User and UserInfo UsersInfo UserId UserName Email Password LastLoginDate User UserId FirstName

I have two tables like User and UserInfo

  • UsersInfo

    UserId

    UserName

    Email

    Password

    LastLoginDate

  • User

    UserId

    FirstName

    LastName

    PhoneNumber

I would like to get data from database using join. i have written following query to get result from both above table.

public ViewResult Index()
        {
            List<UserDetailModel> UsersCompleteDetails = (from u in db.Users
                                                          join l in db.UsersInfoes on u.UserId equals l.UserId
                                                          select new { u.UserId, u.UserName, u.Email, u.Password, u.LastLoginDate, l.FirstName, l.LastName, l.PhoneNumber }).ToList<UserDetailModel>;
            return View(UsersCompleteDetails);
        }

and following is the Custom Class Model

**public class UserDetailModel
{
    //Following details are comes from UsersInfo Table
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime LastLoginDate { get; set; }
    //Following details are comes f开发者_运维知识库rom User Table
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
}**

but do not have any idea about how could i map the custom class with query result? right now it is giving me following Error:

Cannot convert method group 'ToList' to non-delegate type 'System.Collections.Generic.List'. Did you intend to invoke the method?


The specific error you are getting is because you don't have parenthesis after the call to ToList ie: ToList<...>(). However you have a larger issue in that you are trying to convert a list of an anonymous type to a typed list.

You could use a number of libraries out there to help you map your Entities to Model objects and vice versa like AutoMapper but you could also just create conversion methods on the model and data objects themselves or use automatic property initialization which would be similar to your generation of the anonymous type.

The example below should work for your case, it isn't formatted pretty but should get the point across.

public ViewResult Index()
{
    List<UserDetailModel> UsersCompleteDetails = (from u in db.Users
                                                  join l in db.UsersInfoes on u.UserId equals l.UserId into userInfo
                                                  from info in userInfo.DefaultIfEmpty()
                                                  select new UserDetailModel() {  
        UserId = u.UserId, 
        UserName = u.UserName, 
        Email = u.Email, 
        Password = u.Password, 
        LastLoginDate = u.LastLoginDate, 
        FirstName = l.FirstName, 
        LastName = l.LastName, 
        PhoneNumber = l.PhoneNumber 
    }).ToList();

    return View(UsersCompleteDetails);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消