繁体   English   中英

使用单个linq查询汇总两个表中的数据

[英]Aggregate data from two tables using single linq query

我对LINQ还是很陌生,还不了解它的全部功能。

我有两个如下表

Links
----------------------
LinkId 
LinkTitle 
DateAdded


Articles
----------------------
ArticleId
ArticleTitle
DateAdded

我正在尝试使用单个linq查询创建结果集,该查询集将输出如下数据

Year
Month
NoOfLinksAdded
NoOfArticleAdded

任何帮助将不胜感激。

更新:那就是我到目前为止所做的

var grouped = from p in Links 
              group p by new { month = p.DateAdded.Month,year= p.DateAdded.Year } into d 
              select new { Month = d.Key.month, Year= d.Key.year, NoOfLinksAdded = d.Count() };

这是在单个查询中执行此操作的方法,但这不是很漂亮...

var query = from n in Enumerable.Range(0, 1) //we need something to start our query from
            //shape the articles so we can append them to the links 
            let articlesShaped = from a in articles
                                 select new
                                 {
                                     IsLink = false,
                                     IsArticle = true,
                                     a.DateAdded,
                                 }
            //shape the links so we can append them to the articles
            let linksShaped = from l in links
                              select new
                              {
                                  IsLink = true,
                                  IsArticle = false,
                                  l.DateAdded,
                              }
            //append the links and articles together
            let articlesAndLinks = articlesShaped.Concat(linksShaped)
            from a in articlesAndLinks
            group a by new { a.DateAdded.Month, a.DateAdded.Year } into grouping
            select new
            {
                grouping.Key.Year,
                grouping.Key.Month,
                NoOfLinksAdded = grouping.Where(a1 => a1.IsLink).Count(),
                NoOfArticleAdded = grouping.Where(a1 => a1.IsArticle).Count(),
            };

结果如下:

这是输出

如果将其拆分为单个步骤,它将更具可维护性。 这也不会花费任何性能,因为执行仅在由于延迟执行而对结果集进行迭代之后才发生。

如果我们将代码分成几步,它将变成以下内容(我认为这要好得多)

//shape the articles so we can append them to the links                   
var articlesShaped = from a in articles
                     select new
                     {
                         IsLink = false,
                         IsArticle = true,
                         a.DateAdded,
                     };

//shape the links so we can append them to the articles
var linksShaped = from l in links
                  select new
                  {
                      IsLink = true,
                      IsArticle = false,
                      l.DateAdded,
                  };

//append the links and articles together
var articlesAndLinks = articlesShaped.Concat(linksShaped);

var query = from a in articlesAndLinks
            //group by the month and year
            group a by new { a.DateAdded.Month, a.DateAdded.Year } into grouping
            select new
            {
                grouping.Key.Year,
                grouping.Key.Month,
                //get the number of links
                NoOfLinksAdded = grouping.Where(a1 => a1.IsLink).Count(),
                //get the number of articles
                NoOfArticleAdded = grouping.Where(a1 => a1.IsArticle).Count(),
            };

最后,这是您可以运行的完整版本,因此您可以查看运行中的代码:

//make some test data
var links = new []
{
    new    
    {
        LinkId = 1,
        LinkTitle = "A link",
        DateAdded = new DateTime(2012, 5, 1),
    },
    new
    {
        LinkId = 2,
        LinkTitle = "Another link",
        DateAdded = new DateTime(2012, 5, 1),
    },
    new
    {
        LinkId = 3,
        LinkTitle = "A link bro!",
        DateAdded = new DateTime(2012, 6, 1),
    },
    new
    {
        LinkId = 4,
        LinkTitle = "A link dude",
        DateAdded = new DateTime(2012, 6, 1),
    },
    new
    {
        LinkId = 5,
        LinkTitle = "A link man!",
        DateAdded = new DateTime(2012, 7, 1),
    },
};
//make some test data
var articles = new []
{
    new
    {
        ArticleId = 1,
        ArticleTitle = "An article",
        DateAdded = new DateTime(2012, 6, 1),
    },
    new
    {
        ArticleId = 2,
        ArticleTitle = "An article",
        DateAdded = new DateTime(2012, 6, 1),
    },
    new
    {
        ArticleId = 3,
        ArticleTitle = "An article",
        DateAdded = new DateTime(2012, 7, 1),
    },
    new
    {
        ArticleId = 4,
        ArticleTitle = "An article",
        DateAdded = new DateTime(2012, 8, 1),
    },
};

//shape the articles so we can append them to the links                   
var articlesShaped = from a in articles
                     select new
                     {
                         IsLink = false,
                         IsArticle = true,
                         a.DateAdded,
                     };
//shape the links so we can append them to the articles
var linksShaped = from l in links
                  select new
                  {
                      IsLink = true,
                      IsArticle = false,
                      l.DateAdded,
                  };
//append the links and articles together
var articlesAndLinks = articlesShaped.Concat(linksShaped);

var query = from a in articlesAndLinks
            //group by the month and year
            group a by new { a.DateAdded.Month, a.DateAdded.Year } into grouping
            select new
            {
                grouping.Key.Year,
                grouping.Key.Month,
                //get the number of links
                NoOfLinksAdded = grouping.Where(a1 => a1.IsLink).Count(),
                //get the number of articles
                NoOfArticleAdded = grouping.Where(a1 => a1.IsArticle).Count(),
            };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM