我有这样的查询:
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
操作员写这个。我该怎么做?
谢谢
我有这样的查询:
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
操作员写这个。我该怎么做?
谢谢
我认为没有充分的理由在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 };