在单独的方法中转换查询ToListAsync,而不是ActionResult

时间:2014-03-13 17:19:17

标签: entity-framework-6

我正在尝试转换'使用async将列表中的值检索到List并将NewsListViewModel作为成功或失败返回到我的控制器,如下所示:

var model = await db.News.Where(n => n.newsPublished == false
                && n.newsTitle.Contains(query)
                || n.newsDescription.Contains(query)).
                OrderBy(n => n.newsCreatedDate).Select(n => new List<NewsListViewModel>
                {
                    NewsCreatorFName = n.Profile.profileFirstName,
                    NewsCreatorLName = n.Profile.profileLastName,
                    NewsID = n.newsID,
                    NewsCreatorID = n.userID,
                    NewsTitle = n.newsTitle,
                    CreatedDate = n.newsCreatedDate
                })
                .ToListAsync();

或者像这样:

public static async Task<List<NewsListViewModel>> GetAllNotPublished(string query)
    {
        if (query != "" && query != null)
        {

            var model = (from n in db.News
                         where n.newsPublished == false && n.newsTitle.Contains(query)
                         || n.newsDescription.Contains(query)
                         orderby n.newsCreatedDate
                         select new NewsListViewModel
                         {
                             NewsCreatorFName = n.Profile.profileFirstName,
                             NewsCreatorLName = n.Profile.profileLastName,
                             NewsID = n.newsID,
                             NewsCreatorID = n.userID,
                             NewsTitle = n.newsTitle,
                             CreatedDate = n.newsCreatedDate
                         });


            return await model.ToListAsync();

        }
        else
        {
            var model = (from n in db.News
                         where n.newsPublished == false
                         orderby n.newsCreatedDate
                         select new NewsListViewModel
                         {
                             NewsCreatorFName = n.Profile.profileFirstName,
                             NewsCreatorLName = n.Profile.profileLastName,
                             NewsID = n.newsID,
                             NewsCreatorID = n.userID,
                             NewsTitle = n.newsTitle,
                             CreatedDate = n.newsCreatedDate
                         });

            return await model.ToListAsync();

        }

    }

我可能正在考虑这个低音和WAAAAY错误,但我一直在努力争取这几个小时,现在试图找出它并且找不到任何看起来相似的例子。 有一件事是我可以返回任何一个,如果它在ActionResult中,这对我来说很奇怪。以下工作正常,但我不想在ActionResult中使用它。我想在单独的方法中使用列表检索部分。 提前感谢您的帮助。

public async Task<ActionResult> PublishList(string query)
    {

        if (query != "" && query != null)
        {

            var model = (from n in db.News
                            where n.newsPublished == false && n.newsTitle.Contains(query)
                            || n.newsDescription.Contains(query)
                            orderby n.newsCreatedDate
                            select new NewsListViewModel
                            {
                                NewsCreatorFName = n.Profile.profileFirstName,
                                NewsCreatorLName = n.Profile.profileLastName,
                                NewsID = n.newsID,
                                NewsCreatorID = n.userID,
                                NewsTitle = n.newsTitle,
                                CreatedDate = n.newsCreatedDate
                            });


            return View(await model.ToListAsync());

        }
        else
        {
            var model = (from n in db.News
                            where n.newsPublished == false
                            orderby n.newsCreatedDate
                            select new NewsListViewModel
                            {
                                NewsCreatorFName = n.Profile.profileFirstName,
                                NewsCreatorLName = n.Profile.profileLastName,
                                NewsID = n.newsID,
                                NewsCreatorID = n.userID,
                                NewsTitle = n.newsTitle,
                                CreatedDate = n.newsCreatedDate
                            });


            return View(await model.ToListAsync());

        }


    }

0 个答案:

没有答案