Question

I have recently been running through the Dapper extension library for my API. the following code returns a list of users with their resptive addresses based on a single user id:

 public User GetUserBicProfiles(string userId)
    {

        using (
            var multipleResults = this.db.QueryMultiple("sp_UserPermisisons", new {userId = 
           userId},commandType: CommandType.StoredProcedure))
         {
            var user = multipleResults.Read<User>().SingleOrDefault();

            var permissions= multipleResults.Read<Permissions>().ToList();

            if (user != null&& permissions != null)
            {
               user.Permissions.AddRange(permissions);
            }

            return user;
        }
    }

this works perfectly, but when I try and use the same set of details, but without the userId as the parameter (should return all users with their respective permissions), I get an error relating to adding the range.

System.Collections.Generic.List' does not contain a definition for 'UserPermissions' and no extension method 'UserPermissions' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

The method to return all is:

public List<User> GetUsers()
    {
        using (var multipleResults = this.db.QueryMultiple("sp_UserPermissions",commandType: CommandType.StoredProcedure))
        {
            var user = multipleResults.Read<User>().ToList();

            var permissions = multipleResults.Read<Permissions>().ToList();

            if (user != null && permissions != null)
            {

                user.Permissions.AddRange(permissions);
            }

            return user;
        }
    }

the User class is:

public class User
{
    public User()
    {
        this.Permissions = new List<Permissions>();
    }
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string ApplicationName { get; set; }
    public string Email { get; set; }
    public string Comment { get; set; }
    public string Password { get; set; }
    public string PasswordQuestion { get; set; }
    public string PasswordAnswer { get; set; }
    public bool? IsApproved { get; set; }
    public DateTime LastActivityDate { get; set; }
    public DateTime LastLoginDate { get; set; }
    public DateTime LastPasswordChangeDate { get; set; }
    public DateTime CreationDate { get; set; }
    public bool? IsOnline { get; set; }
    public bool? IsLockedOut { get; set; }
    public DateTime LastLockedOutDate { get; set; }
    public bool? FailedPasswordAttempCount { get; set; }
    public DateTime FailedPasswordAttemptWithStart { get; set; }
    public bool? FailedPasswordAnswerAttemptCount { get; set; }
    public DateTime FailedPasswordAnsdwerAttemptWindowStart { get; set; }


    public List<Permissions> Permissions { get; set; } 
}
Was it helpful?

Solution

The error is because you're trying to access the Permissions object on a List<User> object, which does not exist either as property or as an extensions method.

In your first example, the variable user is of type User, you return a single User object by using the SingleOrDefault() method.

...
var user = multipleResults.Read<User>().SingleOrDefault();

var permissions= multipleResults.Read<Permissions>().ToList();
if (user != null&& permissions != null)
{
  user.Permissions.AddRange(permissions);
}

return user; // --> user type is User
...

While in your second example, the variable user is of type List<User>, you return a list of User object (List<User>) using the ToList() method.

...
var user = multipleResults.Read<User>().ToList();
var permissions = multipleResults.Read<Permissions>().ToList();

if (user != null && permissions != null)
{
  user.Permissions.AddRange(permissions);
}

return user; // --> user type is List<User>
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top