开发者

Convert Left outer join query into Entity Framework query

开发者 https://www.devze.com 2023-03-01 12:23 出处:网络
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

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

精彩评论

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