' ArrayList的'不包含' GetAwaiter'的定义

时间:2018-03-13 00:55:58

标签: c# json generics arraylist task

我收到多个错误。因为我对这个async / await进程不熟悉。因此,我很少研究这一点: -

我的功能如下: -

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函数中,我想返回一个arraylists列表并将它们发送到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());

执行睡眠,没有任何反应。没有任何错误或异常生成。

2 个答案:

答案 0 :(得分:2)

从第二种方法结束开始:

return await BulkList;

此处,BulkList被声明为ArrayList。此方法无需以async或以任何方式涉及Task<T>,因此最合适的选项只是从该方法中删除所有asyncTask 。如果您需要将其公开为Task<T> - Task.FromResult可能有用,但它不是最佳的。

答案 1 :(得分:1)

似乎您希望单个函数返回类别列表或板列表。如果电路板和类别不相关(例如,它们不共享公共接口),那么这是一个有问题的设计。呼叫者怎么称呼它?调用者在某些时候必须知道差异,开发人员必须知道,以便将列表放入特定类型的内容中,以便可以读取对象。如果调用者知道差异,为什么不使用两个单独的函数?例如

public async Task<IEnumerable<Category>> FetchCategoriesAsync(AdminBundle abundleList)
{
    if (abundleList.ListType != "Category") throw new ArgumentException("abundleList");
    AdminBundle abundle = Session["AdminBundle"] as AdminBundle;
    abundle.ListType = abundleList.ListType;

    using (SMContext db = new SMContext())
    {
        return await Task<List<Category>>.Run( () => db.CatObj.Where(x => x.Status_Info == Constants.StatusInfoOne).ToList());
    }
}

请注意,在此示例中,db调用包含在一个任务中并等待着。这将为您提供您正在寻找的异步性(一个方法不能执行异步,除非在某处等待它)。

如果您希望能够同时获取类别和电路板,可以在其上实现包装函数,如下所示:

class TableData
{
    public List<Catgeory> Categories { get; set; }       
    public List<Boards> Boards { get; set; }
}

public async Task<TableData> GetTableDataAsync(AdminBundle abundleList)
{
    return new TableData
    {
        Categories = await FetchCategoriesAsync(abundleList),            
        Boards = await FetchBoardsAsync(abundleList);
    };
}