Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework and CROSS/OUTER APPLY

I want to create some test cases for Entity Framework queries that surely generate SQL commands that contain CROSS APPLY or OUTER APPLY operators.

Could someone show typical scenarios where these kind of SQL queries appear?

like image 370
tamasf Avatar asked May 11 '13 19:05

tamasf


People also ask

What is cross apply and outer apply in SQL?

So you might conclude, the CROSS APPLY is equivalent to an INNER JOIN (or to be more precise its like a CROSS JOIN with a correlated sub-query) with an implicit join condition of 1=1 whereas the OUTER APPLY is equivalent to a LEFT OUTER JOIN.

Is Outer apply same as LEFT join?

OUTER APPLY resembles LEFT JOIN, but has an ability to join table-evaluated functions with SQL Tables. OUTER APPLY's final output contains all records from the left-side table or table-evaluated function, even if they don't match with the records in the right-side table or table-valued function.

What is Outer apply?

The OUTER APPLY operator returns all the rows from the left table expression regardless of its match with the right table expression. For those rows for which there are no corresponding matches in the right table expression, it returns NULL values in columns of the right table expression.

Why we use outer apply?

Using the OUTER APPLY OperatorIf you still want the row from the outer table to be included in the result set, you should use the OUTER APPLY operator. The OUTER APPLY operator returns all rows from the outer table, whether or not the function returns data for a specific row.


1 Answers

In LINQ 2 SQL this always results in an APPLY:

from t1 in tab1
from t2 in tab2.Where(t2 => t2.SomeCol == t1.SomeCol).Take(1)
select new { t1, t2 }

In EF this will either fail, or also result in an APPLY (I don't know which one). This is a correlated join which requires an APPLY on the SQL side.

like image 187
usr Avatar answered Sep 18 '22 18:09

usr