简体   繁体   English

无法将类型'bool'隐式转换为'system.threading.tasks.task bool'

[英]Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'

I have this error: "Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'" in my service implementation code. 我有这个错误:“不能在我的服务实现代码中隐式地将类型'bool'转换为'system.threading.tasks.task bool'”。 Could you correct my code please. 你能纠正我的代码吗?

public Task<bool> login(string usn, string pwd)
    {
        DataClasses1DataContext auth = new DataClasses1DataContext();
        var message = from p in auth.Users
                      where p.usrName == usn && p.usrPass == pwd
                      select p;
        if (message.Count() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

You need to be specific whether you want this operation happen asynchronously or not. 您需要具体说明是否要将此操作异步发生。

As an example for Async Operation : 作为Async Operation的示例:

public async Task<bool> login(string usn, string pwd)
{
    DataClasses1DataContext auth = new DataClasses1DataContext();
    var message = await (from p in auth.Users
                  where p.usrName == usn && p.usrPass == pwd
                  select p);
    if (message.Count() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

If you don't need it to be an Async operation, try this: 如果您不需要它作为异步操作,请尝试以下操作:

public bool login(string usn, string pwd)
{
    DataClasses1DataContext auth = new DataClasses1DataContext();
    var message = from p in auth.Users
                  where p.usrName == usn && p.usrPass == pwd
                  select p;
    if (message.Count() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Note: async and await are compatible with .net 4.5 and C# 5.0 and more 注意: asyncawait与.net 4.5和C#5.0及更高版本兼容

If you add Task.FromResult , you can fake it into compiling and working even though your method is not async . 如果添加Task.FromResult ,即使您的方法不是async ,也可以将其伪装成编译和工作。 I had to do this when hooking up Identity, which is all async , to a legacy back end. 我必须在将Identity( async )连接到传统后端时执行此操作。

Example: 例:

public override Task<bool> IsEmailConfirmedAsync(string userId)
{
  var profile = UserProfileType.FetchUserProfile(AtlasBusinessObject.ClientId.ToString(), decimal.Parse(userId));
  Task.FromResult(profile.EmailAddress.NullIfEmpty() != null);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;bool&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task' 无法将布尔类型隐式转换为System.Threading.Tasks.Task <bool> - Cannot implicitly convert type bool to System.Threading.Tasks.Task<bool> 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 无法将类型void隐式转换为System.Threading.Tasks.Task <bool> - Cannot implicity convert type void to System.Threading.Tasks.Task<bool> 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;System.Threading.Tasks.Task &lt;&gt;&gt; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>> 无法隐式转换类型&#39;System.Threading.Tasks.Task <String> “串” - Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string' 无法将类型“System.Threading.Tasks.Task”隐式转换为“Windows.Foundation.IAsyncAction”。 存在显式转换 - Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Windows.Foundation.IAsyncAction'. An explicit conversion exists 无法隐式转换类型&#39;System.Threading.Tasks.Task <object> 在SignalR中从&#39;到&#39;string&#39; - Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR 错误 CS0029:无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> '</string> - Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM