Home > database >  ASP.NET Core - Get tuple with two values
ASP.NET Core - Get tuple with two values

Time:02-08

I just want to get the user's role and ID from the user table. But I do not know why the following code gives an error!!!

public Tuple<Guid,Guid> GetUserId(string mobile)
{
    return _context.Users.Select(u => new Tuple<Guid,Guid,Boolean>(u.UserId,u.RoleId,u.Mobile == mobile));
}

I searched a lot but did not find an answer?

Error:

error image

CodePudding user response:

Meanwhile, your existing code will return IEnumerable<Tuple<Guid,Guid>> or IQueryable<Tuple<Guid,Guid>>, which un-match with the method return type, Tuple<Guid,Guid>.

Assume that you are trying to filter with mobile.

You need either of these methods:

to return (single) result Tuple<Guid,Guid>.

public Tuple<Guid,Guid> GetUserId(string mobile)
{
    return _context.Users
        .Where(u => u.Mobile == mobile)
        .Select(u => new Tuple<Guid,Guid>(u.UserId,u.RoleId))
        .First();
}

CodePudding user response:

you are getting errors for casting as you are converting boolean to string.

u.Mobile == mobile

it will return Boolean value, not a string.

correct code

 public Tuple<Guid,Guid> GetUserId(string mobile)
    {
        return _context.Users.Select(u => new Tuple<Guid,Guid,Boolean>(u.UserId,u.RoleId,u.Mobile == mobile));
    }

CodePudding user response:

You have problem with your method defíntion. Your method's return type is Tuple<Guid,Guid> but you returnig values as Tuple<Guid,Guid,Boolean> which it is not matching.

Just update the method return tpye tupple as:

public Tuple<Guid,Guid, Boolean> GetUserId(string mobile)
{
    return _context.Users.Select(u => new Tuple<Guid,Guid,Boolean>(u.UserId,u.RoleId,u.Mobile == mobile));
}
  •  Tags:  
  • Related