.net 我可以在查询中执行GroupBy以避免poco类中的属性吗?

.net 我可以在查询中执行GroupBy以避免poco类中的属性吗?,.net,sql,linq,poco,dto,.net,Sql,Linq,Poco,Dto,我在db中有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对象: pub

我在db中有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 int NewsID{get;set;}
public int NewsTypeID{get;set;}
public int NewsFormatID{get;set;}
公共字符串类别{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
}
公共类新闻类别
{
公共字符串标题{get;set;}
公共列表新闻{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();
        }
    }
公共静态列表GetNews()
{
使用(var tc=NewsDataContext())
{
var dc=tc.DataContext;
var newsQuery=(来自dc.news中的新闻
在dc中加入NewsCategories on news.NewsCategorid等于newsCategory.NewsCategorid
在dc中加入新闻类型。新闻类型在新闻上。新闻类型ID等于新闻类型。新闻类型ID
在dc.newsFormat中加入newsFormat on news.NewsFormatID等于newsFormat.NewsFormatID
选择新新闻
{
NewsID=news.NewsID,
NewsFormatID=newsFormat.NewsFormatID,
标题=新闻。标题,
Description=新闻。Description,
类别=新闻类别。标题
});
返回newsQuery.GroupBy(item=>item.Category)。选择(item=>new NewsCategory
{
Caption=item.Key,
News=item.ToList()
}).ToList();
}
}
…这是有效的。我这里的问题是,我可以从POCO类新闻中删除NewsCategory属性,并直接在查询中按类别进行分组(在联接之后),以及如何??
提前谢谢

您可以在新闻POCO中添加NewsCategory属性,并将其延迟/显式加载


查看post了解更多详细信息。

此代码演示如何在执行查询后加载行/列数据并对其进行整形。它不“在数据库中分组”,但允许删除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;
var查询=(
来自华盛顿的新闻
从news.NewsCategories中的NewsCategories
让newsFormat=news.newsFormat
选择new//anon类型
{
NewsID=news.NewsID,
NewsFormatID=newsFormat.NewsFormatID,
标题=新闻。标题,
Description=新闻。Description,
类别=新闻类别。标题
});
//加载一行
var rows=query.ToList();
//将anon行塑造成我们的POCSO。
列表结果=行
.GroupBy(item=>item.Category)
.Select(g=>新新闻类别()
{
标题=g.键,
News=g.Select(行=>newnews()
{
NewsID=row.NewsID,
NewsFormatID=行。NewsFormatID,
Caption=row.Caption,
Description=行。Description
}
})
.ToList();
返回结果;

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


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

我正在使用Linq2Sql。还有其他想法吗?我仍然希望在db中按类别对新闻进行分组,并将NewsCategory中的标题作为属性的列表作为我希望对新闻进行分组的依据。如果我理解,为了删除新闻。正确的类别我必须创建匿名类型的对象,按类别分组,然后绑定到适当的POCO?好吧,你不必使用非对象。你也可以声明该对象。它是两个映射(查询映射类型->查询结果类型->POCO)