簡體   English   中英

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

[英]'ArrayList' does not contain a definition for 'GetAwaiter'

我收到了多個錯誤。 因為我是這個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());

執行睡眠,沒有任何反應。 沒有任何錯誤或異常生成。

從第二種方法結束:

return await BulkList;

這里, BulkList被聲明為ArrayList 這種方法不需要以任何方式async或涉及Task<T> ,因此最合適的選擇就是從該方法中刪除所有asyncTask 如果你需要將它作為Task<T>公開 - Task.FromResult可能有用,但它是次優的。

您似乎希望單個函數返回類別列表或板列表。 如果電路板和類別不相關(例如,它們不共享通用接口),那么這是一個有問題的設計。 呼叫者怎么稱呼它? 調用者在某些時候必須知道差異,開發人員必須知道,以便將列表放入特定類型的內容中,以便可以讀取對象。 如果調用者知道差異,為什么不使用兩個單獨的函數? 例如

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);
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM