我有这样的疑问:
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
运算符写这个。我怎么能这样做?
感谢
答案 0 :(得分: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 };