473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SQL to Linq - Left, Right and Inner Joins

Hello,

I am used to SQL but I am starting to use LINQ.

How can I create Left, Right and Inner joins in LINQ? How to
distinguish the different joins?

Here is a great SQL example:
http://www.codinghorror.com/blog/archives/000976.html

Thanks,
Miguel
Jul 3 '08 #1
9 11899
Frans Bouma [C# MVP] <pe******************@xs4all.nlwrote:
Michel Walsh wrote:
If used alone, yes, but with a WHERE clause, MS SQL Server optimizer should
recognize an (implicit) inner join and thus, limit the effective amount of
data transferred through the wire.

That's not what makes it inefficient. The cross-join execution path is
often less ideal as the cross-join action by itself is first handled,
and THEN the where clause is handled to filter out rows you don't want.
I'd certainly hope that the query optimiser would do the job there
though. For instance, if the where clause is on an indexed column I'd
be *hugely* surprised if the inner join were fully evaluated by SQL
server before looking at the index.

It's not like LINQ passes the join in one SQL statement and then
applies the where clause separately: the query optimiser has the full
query to work with.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 5 '08 #2
To get the SQL LEFT outer join, we can expand the LINQ group join, but for
"a" values having no match, we introduce nulls on the unpreserved side,
like this:

from a in b
join
c in d
on
a.field equals c.otherField
into newSet
from x in newSet.DefaultIfEmpty()

newSet can be any name you want which do not collide with keyword or other
variables.

That is a little bit as saying: if a row in b has disappeared,
re-introduce it supplying null (default) for the unpreserved side, and
now, the 'alias' to use to refer to the unpreserved side is x.
This looks to complicated. A better and simpler way for existing realtions
is to use chained properties

Northwind db = CreateDB();
var query = from e in db.Employees
select new {
Name = e.FirstName,
ReportsTo = e.ParentEmployee.FirstName
};
var list = query.ToList();

which generates left join clause automatically.

Andrus.

Jul 6 '08 #3
Andrus wrote:
>To get the SQL LEFT outer join, we can expand the LINQ group join, but
for "a" values having no match, we introduce nulls on the unpreserved
side, like this:

from a in b
join
c in d
on
a.field equals c.otherField
into newSet
from x in newSet.DefaultIfEmpty()

newSet can be any name you want which do not collide with keyword or
other variables.

That is a little bit as saying: if a row in b has disappeared,
re-introduce it supplying null (default) for the unpreserved side, and
now, the 'alias' to use to refer to the unpreserved side is x.

This looks to complicated. A better and simpler way for existing
realtions is to use chained properties

Northwind db = CreateDB();
var query = from e in db.Employees
select new {
Name = e.FirstName,
ReportsTo = e.ParentEmployee.FirstName
};
var list = query.ToList();

which generates left join clause automatically.
Only with nullable FKs

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 7 '08 #4
In MS SQL Server, the two queries:
-----------------
SELECT au_fname, au_ord
FROM authors, titleauthor
WHERE authors.au_id=titleauthor.au_id
------------------

and

---------------------
SELECT au_fname, au_ord
FROM authors INNER JOIN titleauthor
ON authors.au_id=titleauthor.au_id
-----------------------

produce, both, the SAME query plan. Remember that SQL is not about HOW TO
get the result, but about to DESCRIBE the result you want. The HOW TO, the
imperative part, is ideally left to the database.
So, yes, if the query plan would use a cross join to solve the problem, that
would be inefficient, but as writing the description of what I want, there
is NO PROBLEM using a CROSS JOIN to do it, FOLLOWED by a WHERE clause.
When I said that SQL joins are probably easier to understand by STARTING
with cross joins, I meant as a mean to understand them, not HOW the database
should (would) do it. In fact, I am far from the first using that approach,
indeed, I 'borrow' the approach from Joe Celko. (The outer joins are then
explained by the re-injection of each row, from the preserved side, which
would have disappear.)
LINQ can thus allow easy non-equi-join if you start by describing a cross
join, and the use the WHERE clause to describe the condition that you would
normally put in the ON clause of the INNER join. The problem is for OUTER
NON EQUI join, only. In some cases, you can still make use of sub-queries.

Vanderghast, Access MVP

"Frans Bouma [C# MVP]" <pe******************@xs4all.nlwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Michel Walsh wrote:
>If used alone, yes, but with a WHERE clause, MS SQL Server optimizer
should recognize an (implicit) inner join and thus, limit the effective
amount of data transferred through the wire.

That's not what makes it inefficient. The cross-join execution path is
often less ideal as the cross-join action by itself is first handled, and
THEN the where clause is handled to filter out rows you don't want. As a
cross-join can spawn millions of tuples, which are often not needed, the
where clause will run slower than an ON clause in an inner-join set
operation.

It's not the data that's sent over the wire, it's the query execution
itself which makes it less ideal. That's also why I find it a big design
flaw in linq that the only operator in the linq's join clause is an
'equals' operator, which makes the production of left/right joins awkward,
yet these operations are often required. The alternative is a nested from
but that leads to lesser ideal queries than a left-join + accompanying on
clause.

FB
>>

Jul 7 '08 #5

"Frans Bouma [C# MVP]" <pe******************@xs4all.nlwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
(...)
Btw, The cross joins in MS code often rely on a T-SQL specific statement
'CROSS APPLY'
(...)
Hum, are you sure? CROSS APPLY is allowing an ON clause based on the
evaluation of a 'function' returning one or multiple rows (the rows are
thus supplied by the function rather than by a db.table). Furthermore, only
with MS SQL Server 2005. It did not exist before 2005, while CROSS JOIN
existed long time before. So CROSS JOIN can hardly be said to be based on
CROSS APPLY.
You were probably having something else in mind.

Vanderghast, Access MVP


Jul 7 '08 #6
There is only one table! There is no join, so no outer join either :-)

You probably pasted the wrong code snippet.
Vanderghast, Access MVP
"Andrus" <ko********@hot.eewrote in message
news:OX**************@TK2MSFTNGP05.phx.gbl...
>To get the SQL LEFT outer join, we can expand the LINQ group join, but
for "a" values having no match, we introduce nulls on the unpreserved
side, like this:

from a in b
join
c in d
on
a.field equals c.otherField
into newSet
from x in newSet.DefaultIfEmpty()

newSet can be any name you want which do not collide with keyword or
other variables.

That is a little bit as saying: if a row in b has disappeared,
re-introduce it supplying null (default) for the unpreserved side, and
now, the 'alias' to use to refer to the unpreserved side is x.

This looks to complicated. A better and simpler way for existing realtions
is to use chained properties

Northwind db = CreateDB();
var query = from e in db.Employees
select new {
Name = e.FirstName,
ReportsTo = e.ParentEmployee.FirstName
};
var list = query.ToList();

which generates left join clause automatically.

Andrus.

Jul 7 '08 #7
There are two tables !

e.ParentEmployee !

There is join, left outer join in case of nullable fk as Frans responds.

I pasted correct snippet.

Andrus.
There is only one table! There is no join, so no outer join either :-)

You probably pasted the wrong code snippet.
Jul 7 '08 #8
I only see one FROM clause, one table, aliased as e:
var query = from e in db.Employees
select new {
Name = e.FirstName,
ReportsTo = e.ParentEmployee.FirstName}

and e.ParentEmployee.FirstName sounds like part of an undefined relation (at
least, undefined in the code snippet). Surely you will agree that a join CAN
occur over tables which have no predefined relation between them.

Vanderghast, Access MVP

"Andrus" <ko********@hot.eewrote in message
news:OB**************@TK2MSFTNGP03.phx.gbl...
There are two tables !

e.ParentEmployee !

There is join, left outer join in case of nullable fk as Frans responds.

I pasted correct snippet.

Andrus.
>There is only one table! There is no join, so no outer join either :-)

You probably pasted the wrong code snippet.

Jul 7 '08 #9
Ah, just saw Frans Bouma's post.
Vanderghast, Access MVP
Jul 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
2
by: tricard | last post by:
Good day all, I have a large outer joined query that I want to have some criteria. The select query is gathering all part numbers from tblPartNumbers, left joining to tblPartNumberVendor (since...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
4
by: shapper | last post by:
Hello, I have 2 tables: Aid, Aname ... Bid, Aid, Bname ... I need to get the records in B given a Bname and a Aname. I think I should use Inner Join. I wrote the following code:
7
by: =?Utf-8?B?VG9tIEJvbWJhZGls?= | last post by:
Question: Can an outer non-equijoin be simulated using LINQ? It's quite unfortunate that LINQ doesn't support literal joins based on anything other than equality. I know that this the most common...
8
by: Alcides | last post by:
Hello all, I learn about LINQ here in this forum. I been a VB.NET programmer for quite a while and we are using an internal solution for SQL access. I have some experience with C# and I started...
1
by: silpa | last post by:
Hi, I have an SQL query like this select distinct t1.prodID from Table1 t1 left join Table2 t2 on t2.prodID = t1.prodID left join Table3 t3 on t3.serialno = t2.Id and t3.Qty = 0 ...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.