LINQ to SQL - Left Outer Join with multiple join conditions
Solution 1
You need to introduce your join condition before calling DefaultIfEmpty()
. I would just use extension method syntax:
from p in context.Periods
join f in context.Facts on p.id equals f.periodid into fg
from fgi in fg.Where(f => f.otherid == 17).DefaultIfEmpty()
where p.companyid == 100
select f.value
Or you could use a subquery:
from p in context.Periods
join f in context.Facts on p.id equals f.periodid into fg
from fgi in (from f in fg
where f.otherid == 17
select f).DefaultIfEmpty()
where p.companyid == 100
select f.value
Solution 2
this works too, ...if you have multiple column joins
from p in context.Periods
join f in context.Facts
on new {
id = p.periodid,
p.otherid
} equals new {
f.id,
f.otherid
} into fg
from fgi in fg.DefaultIfEmpty()
where p.companyid == 100
select f.value
Solution 3
I know it's "a bit late" but just in case if anybody needs to do this in LINQ Method syntax (which is why I found this post initially), this would be how to do that:
var results = context.Periods
.GroupJoin(
context.Facts,
period => period.id,
fk => fk.periodid,
(period, fact) => fact.Where(f => f.otherid == 17)
.Select(fact.Value)
.DefaultIfEmpty()
)
.Where(period.companyid==100)
.SelectMany(fact=>fact).ToList();
Solution 4
Another valid option is to spread the joins across multiple LINQ clauses, as follows:
public static IEnumerable<Announcementboard> GetSiteContent(string pageName, DateTime date)
{
IEnumerable<Announcementboard> content = null;
IEnumerable<Announcementboard> addMoreContent = null;
try
{
content = from c in DB.Announcementboards
// Can be displayed beginning on this date
where c.Displayondate > date.AddDays(-1)
// Doesn't Expire or Expires at future date
&& (c.Displaythrudate == null || c.Displaythrudate > date)
// Content is NOT draft, and IS published
&& c.Isdraft == "N" && c.Publishedon != null
orderby c.Sortorder ascending, c.Heading ascending
select c;
// Get the content specific to page names
if (!string.IsNullOrEmpty(pageName))
{
addMoreContent = from c in content
join p in DB.Announceonpages on c.Announcementid equals p.Announcementid
join s in DB.Apppagenames on p.Apppagenameid equals s.Apppagenameid
where s.Apppageref.ToLower() == pageName.ToLower()
select c;
}
// Add the specified content using UNION
content = content.Union(addMoreContent);
// Exclude the duplicates using DISTINCT
content = content.Distinct();
return content;
}
catch (MyLovelyException ex)
{
// Add your exception handling here
throw ex;
}
}
Related videos on Youtube
dan
Updated on August 02, 2021Comments
-
dan almost 3 years
I have the following SQL, which I am trying to translate to LINQ:
SELECT f.value FROM period as p LEFT OUTER JOIN facts AS f ON p.id = f.periodid AND f.otherid = 17 WHERE p.companyid = 100
I have seen the typical implementation of the left outer join (ie.
into x from y in x.DefaultIfEmpty()
etc.) but am unsure how to introduce the other join condition (AND f.otherid = 17
)EDIT
Why is the
AND f.otherid = 17
condition part of the JOIN instead of in the WHERE clause? Becausef
may not exist for some rows and I still want these rows to be included. If the condition is applied in the WHERE clause, after the JOIN - then I don't get the behaviour I want.Unfortunately this:
from p in context.Periods join f in context.Facts on p.id equals f.periodid into fg from fgi in fg.DefaultIfEmpty() where p.companyid == 100 && fgi.otherid == 17 select f.value
seems to be equivalent to this:
SELECT f.value FROM period as p LEFT OUTER JOIN facts AS f ON p.id = f.periodid WHERE p.companyid = 100 AND f.otherid = 17
which is not quite what I'm after.
-
Umar T. almost 7 yearswouldn't it be slower than doing the whole operation in a single linq query?
-
MAbraham1 almost 7 years@umar-t, Yes most likely, considering this was more than eight years ago when I wrote it. Personally I like the correlated sub-query postulated by Dahlbyk here stackoverflow.com/a/1123051/212950
-
Learner over 6 yearsVery useful to see the lambda version!
-
Petr Felzmann over 4 years
.Select(fact.Value)
should be.Select(f => f.Value)
-
Suncat2000 over 4 yearsA "union" is a different operation than a "cross-join". It's like addition vs. multiplication.
-
MAbraham1 over 4 years@Suncat2000, thank you for the correction. Happy Thanksgiving! 👪🦃🙏
-
Frank Thomas about 4 yearsThanks for sharing the .Where qualifier on the from .... defaultifempty statement. I didn't know you could do that.