我遇到了多个错误。因为我是这个异步/等待过程的新手。因此,我几乎没有做任何研究:-
我有一个像这样的函数:-
public async Task<JsonResult> GetMultipleTblResult(AdminBundle aBundleFetch)
{
if (!string.IsNullOrEmpty(aBundleFetch.ListType) && aBundleFetch.ListType.Equals(Constants.Board))
{
ArrayList MainList = new ArrayList();
aBundleFetch.ListType = Constants.Board;
Func<ArrayList> functionBoard = new Func<ArrayList>(() => FetchTableDataAsync(aBundleFetch)); // Getting Error (Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Collections.ArrayList>>' to 'System.Collections.ArrayList')
ArrayList resBoard = await Task.Factory.StartNew<ArrayList>(functionBoard);
aBundleFetch.ListType = Constants.Classes;
Func<ArrayList> functionClass = new Func<ArrayList>(() => FetchTableDataAsync(aBundleFetch)); // Getting Error (Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Collections.ArrayList>>' to 'System.Collections.ArrayList')
ArrayList resClass = await Task.Factory.StartNew<ArrayList>(functionClass);
aBundleFetch.ListType = Constants.ClassSubject;
Func<ArrayList> functionClassSubject = new Func<ArrayList>(() => FetchTableDataAsync(aBundleFetch)); // Getting Error (Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Collections.ArrayList>>' to 'System.Collections.ArrayList')
ArrayList resClassSubject = await Task.Factory.StartNew<ArrayList>(functionClassSubject);
aBundleFetch.ListType = Constants.ClassMaterial;
Func<ArrayList> functionClassMaterial = new Func<ArrayList>(() => FetchTableDataAsync(aBundleFetch)); // Getting Error (Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Collections.ArrayList>>' to 'System.Collections.ArrayList')
ArrayList resClassMaterial = await Task.Factory.StartNew<ArrayList>(functionClassMaterial);
MainList.Add(resBoard);
MainList.Add(resClass);
MainList.Add(resClassSubject);
MainList.Add(resClassMaterial);
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(MainList);
return new JsonResult { Data = json, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
else
return new JsonResult { Data = "", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
我想从我的 FetchTableDataAsync 函数返回一个数组列表列表并将它们发送到 GetMultipleTblResult :-
public async Task<IEnumerable<ArrayList>> FetchTableDataAsync(AdminBundle abundleList)
{
AdminBundle abundle = new AdminBundle();
string innerMesage = string.Empty;
if (Session["AdminBundle"] != null)
abundle = (AdminBundle)Session["AdminBundle"];
ArrayList BulkList = null;
abundle.ListType = abundleList.ListType;
if (!string.IsNullOrEmpty(abundleList.ListType))
{
using (SMContext db = new SMContext())
{
switch (abundleList.ListType)
{
case "Category":
List<Category> CategoryList = null;
CategoryList = db.CatObj.Where(x => x.Status_Info == Constants.StatusInfoOne).ToList();
BulkList.Add(CategoryList);
break;
//Class Starts
case "Board":
List<Board> BoardList = null;
BoardList = db.BoardObj.Where(x => x.Status_Info == Constants.StatusInfoOne).ToList();
BulkList.Add(BoardList);
break;
default:
break;
//Main default Ends
}
}
}
return await BulkList; //Getting Error 'ArrayList' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'ArrayList' could be found (are you missing a using directive or an assembly reference?)
}
基本上,我想从后面的函数 (FetchTableDataAsync) 异步返回一组多个列表到前面的函数 (GetMultipleTblResult),然后将它以 JSON 格式传递给我的 angular.js 文件。
编辑:
所以在@JohnWu 的帮助下我完成了这一点:-
[HttpPost]
[LogInFilter]
public JsonResult GetMultipleTblResult(AdminBundle aBundleFetch)
{
if (!string.IsNullOrEmpty(aBundleFetch.ListType) && aBundleFetch.ListType.Equals(Constants.Board))
{
Task<AllTblListClass> AllTblObj = GetTableDataAsync(aBundleFetch);
//var jsonSerialiser = new JavaScriptSerializer();
//var json = jsonSerialiser.Serialize(AllTblObj);
return new JsonResult { Data = "", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
else
return new JsonResult { Data = "", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public async Task<AllTblListClass> GetTableDataAsync(AdminBundle abundleList)
{
if (!string.IsNullOrEmpty(abundleList.ListType) && abundleList.ListType.Equals(Constants.Board))
{
return new AllTblListClass
{
BoardObj = await FetchBoardsAsync(),
ClsObj = await FetchClassAsync(),
ClsSubObj = await FetchClassSubAsync(),
MatTypeObj = await FetchMaterialTAsync(),
ClassSubMatRelationObj = await FetchClassSubMatRelAsync()
};
}
else
{
return new AllTblListClass { };
}
}
public async Task<List<ClassSubMatRelation>> FetchClassSubMatRelAsync()
{
using (SMContext db = new SMContext())
{
return await Task<List<ClassSubMatRelation>>.Run(() => db.ClassSubMatRelationObj.Where(x => x.Status_Info == Constants.StatusInfoOne).ToList()); // It executes untill here and then sleeps for endless time.
}
} //I'm not writing all functions as it will create a long question
但是在这行代码中:-
return await Task<List<ClassSubMatRelation>>.Run(() => db.ClassSubMatRelationObj.Where(x => x.Status_Info == Constants.StatusInfoOne).ToList());
执行休眠,什么也没有发生。没有任何错误或异常生成。
最佳答案
从第二个方法的结尾开始:
return await BulkList;
在这里,BulkList
声明为 ArrayList
.这个方法不需要是async
或涉及Task<T>
无论如何,所以最合适的选择就是简单地删除所有 async
和 Task
从那个方法。如果您需要将其公开为 Task<T>
- Task.FromResult
可能有用,但不是最理想的。
关于c# - 'ArrayList' 不包含 'GetAwaiter' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49246662/