0

我有这样的查询:

SELECT au_lname,der.col FROM authors INNER JOIN (SELECT t.au_id, COUNT(title_id) AS 'col'
                       FROM titleauthor t GROUP BY t.au_id) der ON authors.au_id=der.au_id

我想用let操作员写这个。我该怎么做?

谢谢

4

1 回答 1

2

我认为没有充分的理由在let这里使用。您可以只使用组加入。

var query = from author in authors
            join title in titleAuthor on author.AuthorId equals title.AuthorId
            into titles
            where titles.Count() != 0
            select new { author.LastName, Count = titles.Count() };

可以在这里使用let计数,我想:

var query = from author in authors
            join title in titleAuthor on author.AuthorId equals title.AuthorId
            into titles
            let count = titles.Count()
            where count != 0
            select new { author.LastName, Count = count };

或者您可以使用原始查询的更直接翻译:

var innerQuery = from title in titleAuthor
                 group title by title.AuthorId into titles
                 select new { AuthorId = titles.Key, Count = titles.Count() };
var query = from author in authors
            join titleCount in innerQuery
              on author.AuthorId equals titleCount.AuthorId
            select new { author.AuthorId, titleCount.Count };
于 2012-06-24T07:32:57.640 回答