ASP.NET 核心 - 获取具有两个值的元组

ASP.NET Core - Get tuple with two values

我只想从用户table那里得到用户的角色和ID。但是不知道为什么下面的代码会报错!!!

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

我搜索了很多但没有找到答案?

错误:

在将布尔值转换为字符串时出现转换错误。

u.Mobile == mobile

它将return布尔值,而不是字符串。

正确的代码

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

您的方法定义有问题。 您方法的 return 类型是 Tuple 但您 returnig 值为 Tuple 它不匹配。

只需更新方法 return tpye tupple 为:

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));
}

同时,您现有的代码将 return IEnumerable<Tuple<Guid,Guid>>IQueryable<Tuple<Guid,Guid>>,其中 un-match 使用方法 return 类型 Tuple<Guid,Guid>

假设您正在尝试使用 mobile 进行过滤。

您需要以下任一方法:

至return(单)结果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();
}