Reputation: 1
I have the following async method inside my asp.net mvc 5 web application with EF6:-
public async Task<Resource> GetResourceByName(string resourcename)
{
return await entities.Resources.SingleOrDefaultAsync(a => a.RESOURCENAME.ToLower() == resourcename.ToLower());
}
now i am trying to get the ID for the returned object from the above method as follow:-
var temp = await GetResourceByName(FQDN).Result.RESOURCEID;
but this will raise the following error:-
Error 1 Cannot await 'long'
now to fix this i did the following :-
var temp = await GetResourceByName(FQDN);
id = temp.RESOURCEID;
so not sure why my first apporach did not work. now i await the Task, and when it finises i will retrieve the .Result and get the ResourceID ... so why this will not work ?
here is the model for the Resource:-
public partial class Resource
{
public long RESOURCEID { get; set; }
public string RESOURCENAME { get; set; }
public Nullable<long> ACQUISITIONDATE { get; set; }
public Nullable<long> WARRANTYEXPIRY { get; set; }
public Nullable<long> EXPIRYDATE { get; set; }
public long COMPONENTID { get; set; }
public string ASSETTAG { get; set; }
public string SERIALNO { get; set; }
public string BARCODE { get; set; }
public Nullable<long> PURCHASELOTID { get; set; }
public Nullable<long> VENDORID { get; set; }
public long RESOURCESTATEID { get; set; }
public Nullable<long> SITEID { get; set; }
public Nullable<long> CIID { get; set; }
public bool ISDEPRECIATIONCONFIGURED { get; set; }
public bool ISDEPRECIATIONCALCULATED { get; set; }
public bool ISPERSONAL { get; set; }
public virtual ComponentDefinition ComponentDefinition { get; set; }
public virtual ResourceLocation ResourceLocation { get; set; }
public virtual ResourceState ResourceState { get; set; }
public virtual SiteDefinition SiteDefinition { get; set; }
}
Upvotes: 0
Views: 190
Reputation: 456457
Awaiting the task will give you the result. If you then want to descend into a property of the result, you'll need to use parenthesis if you want it all in one expression:
var temp = (await GetResourceByName(FQDN)).RESOURCEID;
Upvotes: 1
Reputation: 38077
Calling .Result
on a task blocks the thread until the operation completes. The result object is of the type specified in TResult
for the Task.
Your call did not work, because you can't await the Result of the task, only the task itself. Once you call .Result
there is nothing to await.
Upvotes: 1