Diver Dan
Diver Dan

Reputation: 9963

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 that looks like

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

how do I do this using the entity framework 4?

Upvotes: 2

Views: 978

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364269

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.

Upvotes: 2

Related Questions