0

我的 asp.net mvc Web 应用程序中有以下模型类:

public class SecurityRoleGroupAssign
{
    public  IEnumerable<TechnologyType> TechnologyType {get; set;}
    public  IEnumerable<PermisionLevel> PermisionLevel {get; set;}
    public SecurityRole SecurityRole { get; set; }
    public Nullable<int> rackPermisionLevel { get; set; }
    public Nullable<int> serverPermisionLevel { get; set; }
    public Nullable<int> routerPermisionLevel { get; set; }
    public Nullable<int> virtualMachinePermisionLevel { get; set; }
    public Nullable<int>  switchPermisionLevel { get; set; }
}

但是当我试图填充这个类时,我得到了一个空异常:-

public SecurityRoleGroupAssign populateSecurityRolePermisionLevelAssign(int RoleID)
{
    SecurityRoleGroupAssign c = new SecurityRoleGroupAssign
    {
        SecurityRole = FindAllRole(RoleID),
        PermisionLevel = tms.PermisionLevels.ToList(),
        TechnologyType = tms.TechnologyTypes.ToList(),
        serverPermisionLevel = tms.SecurityroleTypePermisions
         .Where(a=>a.SecurityRoleID == RoleID)
         .Where(a2=>a2.TechnologyType.Name.ToLower() == "server")
         .SingleOrDefault().PermisionLevelID,
        rackPermisionLevel = tms.SecurityroleTypePermisions
         .Where(a => a.SecurityRoleID == RoleID)
         .Where(a2 => a2.TechnologyType.Name.ToLower() == "rack")
         .SingleOrDefault().PermisionLevelID,
        virtualMachinePermisionLevel = tms.SecurityroleTypePermisions
         .Where(a=>a.SecurityRoleID == RoleID)
         .Where(a2=>a2.TechnologyType.Name.ToLower() == "virtual machine")
         .SingleOrDefault().PermisionLevelID,
        routerPermisionLevel = tms.SecurityroleTypePermisions
         .Where(a=>a.SecurityRoleID == RoleID)
         .Where(a2=>a2.TechnologyType.Name.ToLower() == "router")
         .SingleOrDefault().PermisionLevelID,
        switchPermisionLevel = tms.SecurityroleTypePermisions
         .Where(a => a.SecurityRoleID == RoleID)
         .Where(a2 => a2.TechnologyType.Name.ToLower() == "switch")
         .SingleOrDefault().PermisionLevelID
    };
    return c;
}
4

2 回答 2

3

问题是如果不存在项目,SingleOrDefault它将返回null(对于引用类型)。因此,任何时候你打电话.SingleOrDefault().PermisionLevelID都会面临空引用异常的危险。.SingleOrDefault()您可以通过在所选结果之后放置 来解决此问题,例如(... select p.PermissionLevelID).SingleOrDefault().

试试这个:

var permissions = tms.SecurityroleTypePermisions.Where(a => a.SecurityRoleID == RoleID);
SecurityRoleGroupAssign c = new SecurityRoleGroupAssign
{
    SecurityRole = FindAllRole(RoleID),
    PermisionLevel = tms.PermisionLevels.ToList(),
    TechnologyType = tms.TechnologyTypes.ToList(),
    serverPermisionLevel = 
        (from p in permissions
         where p.TechnologyType.Name.ToLower() == "server"
         select p.PermisionLevelID)
        .SingleOrDefault(),
    rackPermisionLevel = 
        (from p in permissions
         where p.TechnologyType.Name.ToLower() == "rack"
         select p.PermisionLevelID)
        .SingleOrDefault(),
    virtualMachinePermisionLevel = 
        (from p in permissions
         where p.TechnologyType.Name.ToLower() == "virtual" 
         select p.PermisionLevelID)
        .SingleOrDefault(),
    routerPermisionLevel = 
        (from p in permissions
         where p.TechnologyType.Name.ToLower() == "router")
         select p.PermisionLevelID
        .SingleOrDefault(),
    switchPermisionLevel =
        (from p in permissions
         where p.TechnologyType.Name.ToLower() == "switch"
         select p.PermisionLevelID
        .SingleOrDefault()
};

或者更好:

var permissions = tms.SecurityroleTypePermisions
                     .Where(a => a.SecurityRoleID == RoleID)
                     .ToLookup(a => a.TechnologyType.Name.ToLower(),
                               a => a.PermisionLevelID);
SecurityRoleGroupAssign c = new SecurityRoleGroupAssign
{
    SecurityRole = FindAllRole(RoleID),
    PermisionLevel = tms.PermisionLevels.ToList(),
    TechnologyType = tms.TechnologyTypes.ToList(),
    serverPermisionLevel = permissions["server"].SingleOrDefault(),
    rackPermisionLevel = permissions["rack"].SingleOrDefault(),
    virtualMachinePermisionLevel = permissions["virtual"].SingleOrDefault(),
    routerPermisionLevel = permissions["router"].SingleOrDefault(),
    switchPermisionLevel = permissions["switch"].SingleOrDefault()
};
于 2013-07-18T22:29:56.947 回答
2

.SingleOrDefault():

返回序列的唯一元素,如果序列为空,则返回默认值;如果序列中有多个元素,此方法将引发异常。

serverPermisionLevel = tms.SecurityroleTypePermisions.Where(a=>a.SecurityRoleID == RoleID).Where(a2=>a2.TechnologyType.Name.ToLower() == "server").SingleOrDefault().PermisionLevelID;

这里的问题是.SingleOrDefault();调用返回了默认的“null”值。当您尝试PermissionLevelID在该空引用上调用时会引发错误。

您可以将其更改为:

 serverPermisionLevel = tms.SecurityroleTypePermisions
.Where(a=>a.SecurityRoleID == RoleID)
.Where(a2=>a2.TechnologyType.Name.ToLower() == "server")
.Select(r => r.PermissionLevelID).SingleOrDefault();

这样,当 .SingleOrDefault 实际返回 null 时,您的可为 null 的整数将被填充,或者PermissionLevelId如果有,则使用实际值填充。

于 2013-07-18T22:31:55.980 回答