I came across following code and don’t know what does having from twice mean in this code. Does it mean there is a join between Books and CSBooks?
List<Product> Books = new List<Product>();
List<Product> CSBooks = new List<Product>();
var AllBooks = from Bk in Books
from CsBk in CSBooks
where Bk != CsBk
select new[] { Bk, CsBk };
It is something like a join. When you say:
that is essentially a more efficient way of writing:
Do you see why? The first one says “hey query processor, customers and orders have a special relationship that is defined by the equality of the customer’s ID and the customer ID stored in an order”. The second one just says “give me the Cartesian product — all the possible combinations of customers and orders — and then filter out the ones that don’t make any sense”. They have the same effect, but the former is more efficient.
However, you can use multiple “from” clauses to do things that are more fancy than just Cartesian products. Suppose a customer can have more than one address:
Multiple
fromclauses are actually a “select many”. That is, they take a sequence, and a way of making sequences from every item of the first sequence, and the merge all the resulting sequences together.The “select many” is simple but extremely powerful; we’ve already seen that you can use a “select many” to make a (slow, but correct) join operation. In fact, you can use select many to make every possible query if you are sufficiently clever and don’t mind wasting a lot of time and memory. For example:
could be written without a “where” like this:
You would be crazy to do so, but
whereandjoinare actually unnecessary — they are just faster, shorter and more efficient ways to write a select many.