开发者

Convert Left outer join query into Entity Framework query

I have a sql statement that I want to be able to convert into EF4.

Its a simple Left outer join t开发者_开发技巧hat looks like

SELECT * 
FROM EntryDate 
LEFT OUTER JOIN Member on Member.CardId = EntryDate.CardID

how do I do this using the entity framework 4?


If there is relation mapped in your model you can simply use navigation properties because they always use left join:

var data = members.EntryDates; 

I expect you don't have such relation because CardId doesn't look like primary key of Member or EntryDate.

If you don't have navigation properties you must use

var query = from m in context.Members
            join e in context.EntryDates on m.CardId equals e.CardId into x
            from res in x.DefaultIfEmpty()
            select new
            {
               Member = m,
               EntryDate = res
            };

This works only in EFv4+ because EFv1 didn't support DefaultIfEmpty.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜