2013-04-25 4 views
3

Я пытаюсь подражать:LINQ; Как выполнить левое внешнее соединение с несколькими условиями?

DB1 AS A LEFT OUTER JOIN 
DB2 AS B 
ON A.[Currency Code] = B.[Currency Code] 
AND A.[Document Date] >= B.[Starting Date] 
AND A.[Document Date] <= B.[Ending Date] 

Это то, что я сейчас:

from ledgers in ledgerEntries 
join currency in currencyExchange 
    on ledgers.CurrencyCode equals currency.CurrencyCode 
     into c 
from currencies in c.DefaultIfEmpty() 
where 
    ledgers.DocumentDate >= currencies.StartingDate 
    && ledgers.DocumentDate <= currencies.EndingDate 

Я прочитал о создании анонимного типа и приравнивая их друг к другу, но это Безразлично работайте при использовании меньше и больше, чем для сравнения дат.

ответ

4

Кажется this Ответ был тем, что я искал.

from ledgers in ledgerEntries 
join currency in currencyExchange 
    on ledgers.CurrencyCode equals currency.CurrencyCode 
     into c 
from currencies in c.Where(currency => currency.StartingDate <= ledgers.DocumentDate 
&& currency.EndingDate <= ledgers.DocumentDate).DefaultIfEmpty() 

В сочетании с this ответ, я был в состоянии упростить:

from ledgers in ledgerEntries 
from currencies in currencyExchange.Where(
    currency => currency.CurrencyCode == ledgers.CurrencyCode 
    && currency.StartingDate <= ledgers.DocumentDate 
    && currency.EndingDate <= ledgers.DocumentDate).DefaultIfEmpty() 
Смежные вопросы