0

我在数据库中有 4 个表:

- News (NewsID(PK), NewsCategoryID(FK), NewsType (FK), NewsFormatID(FK), Caption, Description)
 - NewsType (NewsTypeID (PK), Caption)
 - NewsCategory(NewsCategoryID (PK), Caption)
 - NewsFormat (NewsFormatID (PK), Caption)

我有 2 个 POCO 对象:

public class News
{
    public int NewsID { get; set; }
    public int NewsTypeID { get; set; }
    public int NewsFormatID { get; set; }
    public string Category{ get; set; }
    public string Caption { get; set; }
    public string Description { get; set; }
}

public class NewsCategory
{
    public string Caption { get; set; }
    public List<News> News{ get; set; }
}

我想查询返回列表。在查询中,我想按类别对新闻进行分组。我是这样做的:

public static List<NewsCategory> GetNews()
    {
        using (var tc = new NewsDataContext())
        {
            var dc = tc.DataContext;
            var newsQuery= (from news in dc.News
                          join newsCategory in dc.NewsCategories on news.NewsCategoryID equals newsCategory.NewsCategoryID
                          join newsType in dc.NewsTypes on news.NewsTypeID equals newsType.NewsTypeID
                          join newsFormat in dc.NewsFormat on news.NewsFormatID equals newsFormat.NewsFormatID
                          select new News
                                     {
                                         NewsID = news.NewsID,
                                         NewsFormatID = newsFormat.NewsFormatID,
                                         Caption = news.Caption,
                                         Description = news.Description,
                                         Category = newsCategory.Caption
                                     });

            return newsQuery.GroupBy(item => item.Category).Select(item => new NewsCategory
                                                                             {
                                                                                 Caption = item.Key,
                                                                                 News= item.ToList()
                                                                             }).ToList();
        }
    }

..这是有效的。我的问题是,我可以从 POCO 类 News 中删除 NewsCategory 属性,并直接在查询中(连接后)按类别进行分组吗?如何?提前致谢。

4

2 回答 2

1

此代码演示了如何在执行查询后加载行/列数据并对其进行整形。它不会“在数据库中分组”,但它确实允许删除 News.Category 属性。

var query= (
from news in dc.News
from newsCategory in news.NewsCategories
let newsFormat = news.NewsFormat
select new //anon type
{
  NewsID = news.NewsID,
  NewsFormatID = newsFormat.NewsFormatID,
  Caption = news.Caption,
  Description = news.Description,
  Category = newsCategory.Caption
});

//Load anon rows
var rows = query.ToList();

//shape the anon rows into our POCSO's.
List<NewsCategory> result = rows
  .GroupBy(item => item.Category)
  .Select(g => new NewsCategory()
  {
    Caption = g.Key,
    News = g.Select(row => new News()
    {
      NewsID = row.NewsID,
      NewsFormatID = row.NewsFormatID,
      Caption = row.Caption,
      Description = row.Description
    }
  })
  .ToList();

return result;

您不想“在数据库中分组”是有原因的。group by 的数据库行为是返回键和聚合。您需要键和组元素。为了获取每个组的组元素,linqtosql 将使用每个组的键重新查询数据库。这会导致多次往返(称为 n+1 问题,其中 n 是组数,+1 是获取键的查询)。

Enumerable.GroupBy方法返回组键和元素 - 这正是您想要的。

于 2012-05-02T13:59:29.087 回答
1

您可以在 News POCO 中添加 NewsCategory 属性并延迟/显式加载它。

查看这篇文章了解更多详情。

于 2012-05-02T13:34:53.310 回答