1

私はLINQにまったく慣れておらず、その全機能をまだ認識していません。

私は以下のような2つのテーブルを持っています

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() };
4

1 に答える 1

1

単一のクエリでそれを行う方法は次のとおりですが、あまりきれいではありません...

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(),
            };
于 2012-10-15T12:47:30.823 に答える