Citrus
Citrus

Reputation: 766

Refining an Entity Framework result set using LINQ

I'm trying to refine a set of EF results. I have the following line which grabs all entities:

var results = context.Referrals.ToList();

Which is fine. When I try to conditionally refine the list, I suddenly have an empty list. For example:

if (filterByReferrerName)
   results = results.Where(x => x.ReferrerName.Contains("John Doe")).ToList();

I know there is an item in that initial list with a ReferrerName of "John Doe", but cannot understand why my result set is suddenly empty after that .Where clause.

What am I doing wrong here?

Upvotes: 0

Views: 212

Answers (1)

SHM
SHM

Reputation: 1952

if "ReferrerName" is a complex type on Referral class and you are not using lazy loading you must use eager loading and include that complex type in query, see:

var results = 
      context
     .Referrals
     .Include("ReferrerName")
     .Where(x => x.ReferrerName.Contains("John Doe"))
     .ToList();

hope it helps.

Upvotes: 1

Related Questions