Why can't I directly access the ".SingleAsync (). Property" property?

My test code is:

using (var db = new MyDbContext())
{
  string id = "";
  string pictureUrl = db.UserProfile.Single(x => x.Id == id).PictureUrl; //valid syntax

  var user = await db.UserProfile.SingleAsync(x => x.Id == id); //valid syntax
  string _pictureUrl = user.PictureUrl; //valid syntax
}

My problem: I cannot directly declare pictureUrlas follows:

string pictureUrl = await db.UserProfile.SingleAsync(x => x.Id == id).PictureUrl;

I tried to do this, he threw me an error message:

'Task<UserProfileViewModels>'does not contain a definition for 'PictureUrl'and without an extension method 'PictureUrl'that takes the first argument of a type 'Task<UserProfileViewModels>'.

Can you explain to me why?

+4
source share
1 answer

SingleAsyncreturns a Task<UserProfileViewModels>. This task does not have your property. You need to wait for the task to return the actual result.UserProfileViewModels

, , , :

string pictureUrl = (await db.UserProfile.SingleAsync(x => x.Id == id)).PictureUrl;
+8

Source: https://habr.com/ru/post/1618676/


All Articles