-4

1) Subquery is a query within a query and as such must start with a FROM clause and end with SELECT or GROUP BY clause. But I'm puzzled why don't we also consider as a subquery those nested statements that only have a FROM clause ( thus they don't end with SELECT or GROUP BY ).

The behavior of from c_2 in collection_2 is very similar to the behaviour of a subquery, since it enumerates the entire collection_2 for every c_1 element. Since their behaviours are remarkably similar, why aren't statements such as from c_2 in collection_2 also considered as subqueries ( you could argue that a subquery returns a result, but same argument could also be said of from c_2 in collection_2, since it does get converted into a call to SelectMany )?

    var query = from c_1  in collection_1
                from c_2  in collection_2 
                select ...

thank you

user1483278
  • 929
  • 1
  • 9
  • 17
  • Which part would you consider to be a subquery, when there's only one `select` clause at the end? – Jon Skeet Oct 23 '12 at 18:02
  • @Servy - We don't typically give answers to homework questions on this site, but instead give guidance on how to find the answers. – Tejs Oct 23 '12 at 18:08
  • @Tejs - no it's not a homework. Please elaborate why you think this is a homework – user1483278 Oct 23 '12 at 18:18
  • @Jon Skeet: I never implied that this code contains a subquery ( ie a nested query beginning with from clause and ending with select clause ) – user1483278 Oct 23 '12 at 18:22
  • @user1483278: You certainly implied it: "But I'm puzzled why don't we also consider as a subquery those nested statements that only have a FROM clause". If you're asking a question about one situation, and then giving some sample code, surely there's an implication that the code is an example of your question. – Jon Skeet Oct 23 '12 at 18:32
  • @Jon Skeet: With the code I posted I was giving an example of a nested statement which is not considered a subquery (and was wondering why it isn't considered a subquery). I already explained that general definition of a subquery is that it begins with From and ends with Select/GroupBy clause. Since I explained that, I assumed it is pretty obvious that code I posted doesn't contain a subquery. – user1483278 Oct 23 '12 at 18:41
  • @user1483278: But I asked why you *would* consider it to be a subquery. Your question suggests that you think it *should* be - and I was asking for clarification. – Jon Skeet Oct 23 '12 at 19:03
  • @Jon Skeet: I was curious why nested statement "from c_2 in collection_2" isn't considered a subquery ( thus why would it need to end with select or group by to be considered a subquery ). Why I considered this statement to also be a subquery is explained in my post – user1483278 Oct 23 '12 at 20:11

1 Answers1

1

but same argument could also be said of from c_2 in collection_2, since it does get converted into a call to SelectMany

Not really. That one call doesn't get converted into SelectMany. It's the fact that there are two from clauses, in combination (with just a single select) that causes it to be converted into a SelectMany call, so the whole thing is a single query in that case. If each from matched up with a single select then it would be a subquery.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • So the distinction is whether that piece of code on its own can on its own produce a result ( which "from c_2 in collection_2" can't)? – user1483278 Oct 23 '12 at 18:27
  • 1
    @user1483278 Essentially, yes. Note that a sub-query may or may not use an item from the outer query, meaning it could be evaluated once and then re-used or it could need to be re-evaluated for each item in the outer query. Both cases are still "sub queries". – Servy Oct 23 '12 at 18:28