如何在Linq中返回强类型对象?

如何在Linq中返回强类型对象?,linq,linq-to-sql,linq-to-entities,Linq,Linq To Sql,Linq To Entities,我有一个存储过程,它返回用户列表(用户表中的行) LINQ为我生成了aspnet_用户类,所以我可以将结果映射到aspnet_用户类型列表吗?比如: List<aspnet_User> admins = db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin"); 列表 您完全可能只需要: List<aspnet_User> admins = db.aspnet_UsersInRoles_GetUsersInRoles(

我有一个存储过程,它返回用户列表(用户表中的行)

LINQ为我生成了aspnet_用户类,所以我可以将结果映射到aspnet_用户类型列表吗?比如:

List<aspnet_User> admins =  db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin");

列表

您完全可能只需要:

List<aspnet_User> admins =  db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin")
                              .ToList();

List Admin=db.aspnet\u UsersInRoles\u GetUsersInRoles(“/”,“Admin”)
.ToList();

但是如果不查看方法调用返回的类型,就很难知道结果。

完全有可能您只需要:

List<aspnet_User> admins =  db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin")
                              .ToList();

List Admin=db.aspnet\u UsersInRoles\u GetUsersInRoles(“/”,“Admin”)
.ToList();

但是如果不查看方法调用返回的类型,就很难知道结果。

也许这应该是一条注释,但它太长了

当然,您并不真正想要内部类
,您应该想要一个
成员身份用户

那么,不使用成员资格提供程序附带的存储过程,而是真正使用成员资格提供程序本身如何

有一个漂亮的类:System.Web.Security中的角色

它给了你这个:

public static string[] GetUsersInRole(string roleName)

从这里开始,在列表中获取成员身份用户的foreach就没有那么复杂了。

也许这应该是一个注释,但它太长了

当然,您并不真正想要内部类
,您应该想要一个
成员身份用户

那么,不使用成员资格提供程序附带的存储过程,而是真正使用成员资格提供程序本身如何

有一个漂亮的类:System.Web.Security中的角色

它给了你这个:

public static string[] GetUsersInRole(string roleName)

从这里开始,在列表中获取MembershipUser的foreach就没有那么复杂了。

默认情况下,存储过程将返回一个类型,它将根据输出列确定该类型,并将结果附加到最后。它不会将它与您已经确定的类型相关联。要对此进行更改,您可以将属性窗口中的返回类型更改为您在模型中已定义的类型,或者在将存储过程拖动到模型中时,将其直接放到您希望存储过程映射到的类型上


但是,您没有机会更改存储过程的列映射,因此请确保存储过程生成的形状与目标对象结构相同。

默认情况下,存储过程将返回一种类型,该类型是根据输出列确定的,并将结果附加到最后。它不会将它与您已经确定的类型相关联。要对此进行更改,您可以将属性窗口中的返回类型更改为您在模型中已定义的类型,或者在将存储过程拖动到模型中时,将其直接放到您希望存储过程映射到的类型上


但是,您没有机会更改存储过程的列映射,因此请确保存储过程生成的形状与目标对象结构相同。

这是一篇老文章,我今天正在写,我也遇到了同样的问题

我想你是在申请asp会员资格


无法将此存储过程转换为aspnet\u User,因为它返回aspnet\u UsersInRoles\u GetUsersInRolesResult类型


但是从这个aspnet\u UsersInRoles\u GetUsersInRolesResult可以获得userName,然后请求aspnet\u User表:

    String app = "your application name";
    String role = "your role name";

    ArrayList userInRoleList = new ArrayList();

    //Get the role ID
    ASPNETDBDataContext aspDB = new ASPNETDBDataContext();

    var userInRole = aspDB.aspnet_UsersInRoles_GetUsersInRoles(app, role);

    foreach (aspnet_UsersInRoles_GetUsersInRolesResult users in userInRole)
    {
        userInRoleList.Add(users.UserName);
    }

这是一篇老文章,我今天正在写,我收到了同样的问题

我想你是在申请asp会员资格


无法将此存储过程转换为aspnet\u User,因为它返回aspnet\u UsersInRoles\u GetUsersInRolesResult类型


但是从这个aspnet\u UsersInRoles\u GetUsersInRolesResult可以获得userName,然后请求aspnet\u User表:

    String app = "your application name";
    String role = "your role name";

    ArrayList userInRoleList = new ArrayList();

    //Get the role ID
    ASPNETDBDataContext aspDB = new ASPNETDBDataContext();

    var userInRole = aspDB.aspnet_UsersInRoles_GetUsersInRoles(app, role);

    foreach (aspnet_UsersInRoles_GetUsersInRolesResult users in userInRole)
    {
        userInRoleList.Add(users.UserName);
    }

那么
aspnet\u UsersInRoles\u GetUsersInRoles
返回什么类型?那么
aspnet\u UsersInRoles\u GetUsersInRoles
返回什么类型?aspnet\u UsersInRoles\u GetUsersInRoles返回什么类型ISingleResult@dev.e.loper:嗯。
aspnet\u UsersInRoles\u GetUsersInRolesResult
里面有什么?看起来里面有两个对象结果视图@dev.e.loper:看起来它提供了一系列的
aspnet\u UsersInRoles\u GetUsersInRolesResult
,而不是
aspnet\u User
。。。这些对你来说足够好吗?@Jon Skeet:你不应该完全避免使用aspnet_用户吗?它是成员资格提供程序的内部部分..aspnet\u UsersInRoles\u GetUsersInRoles返回ISingleResult@dev.e.loper:嗯。
aspnet\u UsersInRoles\u GetUsersInRolesResult
中有什么?结果视图中似乎有两个对象@dev.e.loper:看起来它提供了一系列的
aspnet\u UsersInRoles\u GetUsersInRolesResult
,而不是
aspnet\u User
。。。这些对你来说足够好吗?@Jon Skeet:你不应该完全避免使用aspnet_用户吗?它是会员资格提供者的内部部分。。