问题描述:

I know how the following queries work, I'm just confused about the terminology.

Each query begins with mandatory FROM clause and must end with either SELECT or GROUP clause. Between the two clauses we can put any number of optional FROM...LET...WHERE clauses. Subquery is simply a query within a query, which means it too has the same structure:

var result = from p1 in artists

from p3 in (from p2 in p1.albums

select new { ID = p2.id, Title = p2.title })

select p3;

But unlike the subquery in the above example, the following subquery ( from p2 in artist.Albums ) doesn't end with its very own SELECT/GROUP clause, so its structure is different in a sense that it begins with a FROM clause, but doesn't end with SELECT/GROUP clause. So my question is whether the term subquery also applies to from p2 in artist.Albums, even though its structure is a bit different?

 var result1 = from p1 in artists

from p2 in p1.Albums

select new { Artist_id = p1.id, Album_id = p2.id };

thank you

网友答案:

In your second query, result1, there is no subquery, just a query with two from clauses.

Behind the scenes, using two from clauses in this way will be translated into a SelectMany operation:

artists.SelectMany( p1      => p1.albums
                   ,(p1,p2) => new { Artist_id = p1.id, Album_id = p2.id })
网友答案:

No, at the bottom you're doing an implicit join, not a subquery. A join is where you query both tables at the same time (in the same query).

相关阅读:
Top