Skip to main content
 首页 » 编程设计

c#之如何在 DNN 中获取特定的用户信息

2024年02月05日9itcoder

您好,我正在使用 C# 在 DNN 中开发我的模块,我已经使用它检索了用户:

public ArrayList bindingListHere(string txtSearchUser){ 
    string getUsers = txtSearchUser; 
    int totalrecords = 10; 
    Users= UserController.GetUsersByUserName(PortalId, getUsers + "%", 0, 10, ref totalrecords, true, IsSuperUser); 
    return Users; 
} 

我把它绑定(bind)在这里:

protected void Search(object sender, EventArgs e){ 
    //calling the method from the lib that will search user of the portal 
    DownloadCtrLib dctrl = new DownloadCtrLib (); 
    dctrl.bindingListHere (txtSearchUser.Text); 
    gvUser.DataSource = dctrl.bindingListHere (txtSearchUser.Text); 
    gvUser.DataBind(); 
} 

而且它工作正常。它显示有关门户用户的所有信息,例如:

Email  
Firstname  
Lastname  
portalID 

等...

那是我不想要的。因为我只需要用户的UserID、Username和DisplayName。我怎样才能做到这一点?有什么建议吗?

请您参考如下方法:

在您的代码中添加一个新的简单类,它只包含您需要的字段。

public class UserBindings 
{ 
    public int UserID { get; set; } 
    public string Username { get; set; } 
    public string DisplayName { get; set; } 
} 

然后对您的绑定(bind)方法稍作更改:

public List<UserBindings> bindingListHere(string txtSearchUser) 
{ 
    string getUsers = txtSearchUser; 
    int totalrecords = 10; 
    ArrayList Users = UserController.GetUsersByUserName(PortalId, getUsers + "%", 0, 10, ref totalrecords, true, IsSuperUser); 
    return Users.Cast<UserInfo>().Select(u => new UserBindings { UserID = u.UserID, Username = u.Username, DisplayName = u.DisplayName }).ToList(); 
} 

我必须转换 Arraylist 并使用 Linq 将 UserInfo 映射到 UserBinding 对象。现在这个方法将返回一个 UserBinding 列表,它比之前的 UserInfo 对象的 ArrayList 小得多。