在ef core 2.2中将简单代码转换为groupby

时间:2019-05-05 16:11:49

标签: c# entity-framework entity-framework-core

我编写此代码是为了从数据库的每个CategoryId获取6条新闻:

 var FindAllCategory = this.CategoryService.Entities.ToList();
        foreach (var item in FindAllCategory)
        {
            var TakNews = Entities.Where(x => x.CategoryId == item.Id).Take(6).ToList();
            foreach (var addNews in TakNews)
            {
                var model = new NewsDto();
                model.CatId = item.Id;
                model.Id = addNews.Id;
                model.ImagePath = addNews.ImagePath;
                model.Title = addNews.Title;
                model.Description = addNews.Description;
                news.Add(model);
            }
        }

但是我需要使用GroupBy而不是上面的代码。

var TakNewsB = Entities.GroupBy(x => x.CategoryId).Take(6).Select(x => new 
        {
            NewsDto=x.ToList()
        });

但是它不起作用,也没有向我显示任何东西。

这段代码有什么问题?我该如何解决这个问题?

0 个答案:

没有答案