Reputation: 23
I am not able to convert to async action. It says await required
public async Task<JsonResult> Get_Fare_TypeAsync()
{
var types = db.Types.Select(e => new { ID = e.Code, Description = e.Description, Value = e.Code, SortOrder = e.SortOrder }).ToList().OrderBy(x => x.SortOrder);
return Json(types ,JsonRequestBehavior.AllowGet);
}
Upvotes: 0
Views: 98
Reputation: 457057
I am not able to convert to async action. It says await required
You're approaching the problem backwards. You're trying to force Get_Fare_Type
to be asynchronous.
A far better approach is to start at the "other side" - that is, the APIs that you call. Evaluate your API calls and determine whether they have asynchronous alternatives.
Then, start at those APIs, change them to use the asynchronous versions, and use await
. In this example, you would use EF6's ToListAsync
:
public JsonResult Get_Fare_Type()
{
var types = (await db.Types.Select(e => new { ID = e.Code, Description = e.Description, Value = e.Code, SortOrder = e.SortOrder }).ToListAsync()).OrderBy(x => x.SortOrder);
return Json(types, JsonRequestBehavior.AllowGet);
}
Now the compiler will error at you and say you need to make it async
and change the return type to Task<JsonResult>
. This is the next step:
public async Task<JsonResult> Get_Fare_TypeAsync()
{
var types = (await db.Types.Select(e => new { ID = e.Code, Description = e.Description, Value = e.Code, SortOrder = e.SortOrder }).ToListAsync()).OrderBy(x => x.SortOrder);
return Json(types, JsonRequestBehavior.AllowGet);
}
Now, every caller of this method needs to use await
, etc.
Upvotes: 1
Reputation: 3890
To use async you have to use also await.
Here is example:
public async Task<JsonResult> Get_Fare_TypeAsync()
{
var types = db.Types.Select(e => new { ID = e.Code, Description = e.Description, Value = e.Code, SortOrder = e.SortOrder }).ToList().OrderBy(x => x.SortOrder);
return Json(types ,JsonRequestBehavior.AllowGet);
}
To call this method you have to do following:
JsonResult result = await Get_Fare_TypeAsync
More explanation can be found here: Asynchronous Programming with async and await (C#)
Upvotes: 0