开发者

Product of a left outer join in linq not keeping the right side

I'm trying to do a left outer join using linq. I swear this w开发者_StackOverflow中文版orked a couple of weeks ago when I wrote and tested it, but now it's not actually joining the two sides.

var combined = from h in historyTable
join d in (from d in deviceTable where d.DeviceType == 5 select d) on h.SerialNumber equals d.SerialNumber into dh
from subDH in dh.DefaultIfEmpty()
select subDH;

This should work as a left outer join, right? I'm not missing something crucial, as all this is supposed to do is join each table at the SerialNumber. I've made an SQL version, and it works just fine.

SELECT *
FROM [deviceTable] AS [t0]
LEFT OUTER JOIN (
    SELECT *
    FROM [historyTable]
    ) AS [t1] ON [t0].[SerialNumber] = [t1].[SerialNumber]
WHERE [t0].[DeviceType] = 5
GO

What am I doing incorrectly?


It looks like your SQL has the left and right sides round the other way. Try this:

var combined = from d in deviceTable
               where d.DeviceType == 5
               join h in historyTable on d.SerialNumber equals h.SerialNumber
                      into dh
               from subDH in dh.DefaultIfEmpty()
               select new { d, subDH };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜