2013-08-20 56 views
0

使用下面我怎麼就可以這樣寫這個方法沒有錯誤LINQ的類型轉換委託的方法。首先

return _session.Query<Data.Model.User>().Select(_userMapper.CreateUser) 
      .AsQueryable(); 

但寫這

return _session.Query<Data.Model.User>().Where(x => x.Username == username) 
     .First(_userMapper.CreateUser); 

產生錯誤。

public User CreateUser(Data.Model.User modelUser) 
{ 
    return new User{UserId = modelUser.UserId, Username = modelUser.Username, 
        Email = modelUser.Email, Firstname = modelUser.Firstname, 
        Lastname = modelUser.lastname}; 
} 

是否返回集合或單個項目我希望它由create user方法進行轉換。

回答

1

你需要調用SelectFirst()

return _session.Query<Data.Model.User>() 
       .Where(x => x.Username == username) 
       .Select(_userMapper.CreateUser) 
       .First(); 

或者:

return _session.Query<Data.Model.User>() 
       .Select(_userMapper.CreateUser) 
       .First(x => x.Username == username); 
1
return _userMapper.CreateUser(_session.Query<Data.Model.User>().First(x => x.Username == username));