LINQ do SQL-Left Outer Join with multiple join conditions
Mam następujący SQL, który próbuję przetłumaczyć na 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
Widziałem typową implementację lewego połączenia zewnętrznego (tj. into x from y in x.DefaultIfEmpty()
itd.) ale nie jestem pewien, jak wprowadzić drugi warunek przyłączenia (AND f.otherid = 17
)
EDIT
Dlaczego warunek AND f.otherid = 17
jest częścią JOIN zamiast w klauzuli WHERE?
Ponieważ f
może nie istnieć dla niektórych wierszy i nadal chcę, aby te wiersze były włączone. Jeżeli warunek jest zastosowany w klauzuli WHERE, po Dołącz - wtedy nie rozumiem zachowania, którego chcę.
Niestety to:
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
Wydaje się być równoważne temu:
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
Nie do końca o to mi chodzi. 5 answers
Musisz przedstawić swój warunek join przed wywołaniem DefaultIfEmpty()
. Po prostu użyłbym składni metody rozszerzenia:
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
Lub możesz użyć 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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-08 17:03:18
To też działa ...jeśli masz wiele łączy kolumn
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-16 20:59:24
Wiem, że to " trochę za późno ", ale na wszelki wypadek, gdyby ktoś musiał to zrobić w składni metody LINQ (dlatego znalazłem ten post początkowo ), tak by było:
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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-18 17:36:16
Inną ważną opcją jest rozłożenie łączników na wiele klauzul LINQ, Jak Następuje:
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;
}
//CROSS-JOIN this content
content = content.Union(addMoreContent);
//Exclude dupes - effectively OUTER JOIN
content = content.Distinct();
return content;
}
catch (MyLovelyException ex)
{
throw ex;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-14 19:58:33
Wydaje mi się, że jest wartość w rozważaniu niektórych przepisań do kodu SQL przed próbą przetłumaczenia go.
Osobiście napisałbym takie zapytanie jako union (chociaż unikałbym całkowicie null!):
SELECT f.value
FROM period as p JOIN facts AS f ON p.id = f.periodid
WHERE p.companyid = 100
AND f.otherid = 17
UNION
SELECT NULL AS value
FROM period as p
WHERE p.companyid = 100
AND NOT EXISTS (
SELECT *
FROM facts AS f
WHERE p.id = f.periodid
AND f.otherid = 17
);
Więc chyba zgadzam się z duchem odpowiedzi @MAbraham1 (choć ich kod wydaje się być niezwiązany z pytaniem).
Wydaje się jednak, że zapytanie zostało zaprojektowane tak, aby wygenerować wynik pojedynczej kolumny zawierający zduplikowane wiersze-rzeczywiście zduplikowane null! Trudno nie dojść do wniosku, że takie podejście jest wadliwe.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-08 13:57:13