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.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论