【问题标题】:'ArrayList' does not contain a definition for 'GetAwaiter'“ArrayList”不包含“GetAwaiter”的定义
【发布时间】:2018-08-21 03:33:04
【问题描述】:

我遇到了多个错误。因为我是这个异步/等待过程的新手。所以我做了一点研究:-

我有一个类似的功能:-

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

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

【问题讨论】:

  • 请不要使用ArrayList - 几乎没有应该使用的情况,它使代码几乎不可读
  • @MarcGravell 那么我应该将其替换为?哪个错误?
  • 我不知道“睡觉”是什么意思。我建议描述症状(不是初步结论。)
  • 另外,这是什么程序?您需要注意,不同类型的应用程序有不同类型的同步上下文,如果您不小心,其中一些可能会死锁。
  • 表示代码在那之后没有执行..

标签: c# json generics arraylist task


【解决方案1】:

从你的第二种方法结束:

return await BulkList;

这里,BulkList 被声明为ArrayList。此方法无需为async 或以任何方式涉及Task&lt;T&gt;,因此最合适的选择是简单地从该方法中删除所有asyncTask。如果您需要将其公开为Task&lt;T&gt; - Task.FromResult 可能有用,但不是最佳选择。

【讨论】:

  • 感谢@marc,但我需要并行执行此方法
  • BulkList 不是一种方法。它是 ArrayList 的一个实例。您无法执行实例。
  • @Deepak 异步和并行是独立的(但切线相关的)主题。不需要异步/任务并行化
【解决方案2】:

您似乎想要一个函数来返回类别列表或板列表。如果板和类别不相关(例如,它们不共享一个公共界面),那么这是一个有问题的设计。调用者将如何调用它?调用者在某些时候必须知道区别,而开发人员必须知道以便将列表放入特定类型的东西中,以便可以读取对象。如果调用者无论如何都知道区别,为什么不使用两个单独的函数呢?例如

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

【讨论】:

  • 太棒了!有了这个答案,您就完全理解了我的观点!将尝试此操作并尽快回复您。
  • 第一部分实际上我有大约 20 张桌子。为每张桌子制作单独的功能会很长,不是吗?
  • 如果您的域对象共享一个接口,您可以使用通用方法。否则,如果你想要强类型的结果,你需要有特定的返回类型和特定的函数。
  • 谢谢@John 你能分享一个例子吗?
  • 这个Task> FetchCategoriesAsync也不应该是这个Task> FetchCategoriesAsync
猜你喜欢
  • 1970-01-01
  • 2012-08-04
  • 2017-04-20
  • 2017-11-20
  • 2016-12-15
  • 2017-04-08
  • 1970-01-01
  • 2023-01-09
  • 2020-08-21
相关资源
最近更新 更多