简体   繁体   中英

Task does not contain a definition for 'FirstOrDefault'

Error CS1061 'Task>' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'Task>' could be found (are you missing a using directive or an assembly reference?) ,

making a web application with facebook controller, included using system.linq but still getting the error for FirstOrDefault() . I dont know what the problem is, i reinstalled visual studio too as he same thing works for my friends laptop. I juggled around to see if the method is present in the library and it is there but still not getting past this Image for the same

public async Task<ActionResult> Posts()
{
    var currentClaims = UserManager.GetClaimsAsync(HttpContext.Identity.GetUserId());

    // Error occurs here at FirstOrDefault
    var accesstoken = currentClaims.FirstOrDefault(x = > x.Type == "urn:tokens:facebook");

    if (accesstoken == null)
    ...
}

您在UserManager.GetClaimsAsync()前面缺少await

var currentClaims = await UserManager.GetClaimsAsync(...);

Look closely at the error message. GetClaimsAsync returns type Task<IList<Claim>> , not type list. The Task type doesn't have a FirstOrDefault method. As other people have indicated, await GetClaimsAsync and it'll work.

As a side note, do not use .Result here as that will deadlock in most environments. See this article for an explanation of why this will deadlock as well as this article on async/await best practices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM