Geoffrey De Vylder
Geoffrey De Vylder

Reputation: 4153

Conditional Includes

I would like to know how I can change this query:

events = _database.Events
         .Include("Contacts")
         .ToList();

To include only contacts having their property "Type" set to "event".

I'm using EntityFramework, _database is its context.

Upvotes: 2

Views: 89

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502696

Firstly, it's worth understanding that the code you posted doesn't include a lambda expression at all. "Query not in query expression syntax" isn't the same as "lambda expression".

I suspect it would be a bad idea to return event objects with a partially-filled entity reference set. However, you could do this:

_database.Events
         .Select(e => new { Event = e,
                            EventContacts e.Contacts
                                           .Where(c => c.Type == "event") });

Upvotes: 3

Related Questions