如何同时使用 FindByNameAsync 和 FindByIdAsync 检查用户 [英] How to check user with FindByNameAsync and FindByIdAsync both at the same time

查看:49
本文介绍了如何同时使用 FindByNameAsync 和 FindByIdAsync 检查用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何同时检查 id &&用户名同时在这个用户变量中作为第一个参数在 CheckPasswordSignInAsync 方法中传递,谢谢

How to check both id && username in the same time in this user variable to pass it in CheckPasswordSignInAsync method as first paramter and thanks

        var user = await _userManager.FindByNameAsync(userForLoginDto.UserName);
                    await _userManager.FindByIdAsync(userForLoginDto.Id);

推荐答案

将要并行运行的任务传递到 Task.WhenAny 然后得到第一个完成任务的结果:

Pass tasks that you want to run in parallel to Task.WhenAny and then get result of first completed task:

var completedTask = await Task.WhenAny(
     FindByNameAsync(userForLoginDto.UserName),
     FindbyIdAsync(userForLoginDto.Id));
var user = await completedTask;

注意:通常您不应该对同一数据库中的相同数据进行并行搜索.按主键搜索应该是最快的.

Note: usually you should not run a parallel search for the same data in the same database. Search by primary key should be fastest.

如果您想检查用户名是否被其他用户使用,则只需按用户名搜索并检查返回用户的id(如果有).

If you want to check whether user name is taken by another user, then just search by user name and check id of the returned user if any.

更新:如果这些搜索是独立的,并且您只想并行运行它们并获得所有结果,则使用 Task.WhenAll - 当所有任务完成时,它将返回数组或结果:

Update: If those searches are independent and you just want to run them in parallel and get all results, then use Task.WhenAll - it will return array or resuls when all tasks are completed:

var users = await Task.WhenAll(
     FindByNameAsync(userForLoginDto.UserName),
     FindbyIdAsync(userForLoginDto.Id));

这篇关于如何同时使用 FindByNameAsync 和 FindByIdAsync 检查用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆