Taborator
Taborator

Reputation: 79

Why is the Lambda Expression not valid in this Include()?

I'm working with EFCore for the first time, after moving from EF6, and for some reason I keep getting this error:

System.InvalidOperationException: 'Lambda expression used inside Include is not valid.'

Here is my Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        using (var IJ = IJContext.CreateNew(false))
        {
            var ChargeList = iJ.Charges.Where(charge => charge.CourtCase.CaseNumber == "13457894561")
                                             .Include(charge => charge.ChargeDate)
                                             .ToList();
            return View(ChargeList);
        }
    }
}

Am I missing something important here? Does EFCore handle Lambdas totally differently or something?

Upvotes: 1

Views: 7515

Answers (2)

TanvirArjel
TanvirArjel

Reputation: 32069

If you look at the signature of the Include extension method it looks like as follows:

public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>( 
      this IQueryable<TEntity> source,
      Expression<Func<TEntity, TProperty>> navigationPropertyPath)
      where TEntity : class
{
       // Method body
}

The paramter navigationPropertyPath expecting a lambda expression representing the navigation property to eager load the data for that navigation property. But you are passing the entity property ChangeDate which is not navigation property.

For more details: EF Core Eager loading

Upvotes: 0

Athanasios Kataras
Athanasios Kataras

Reputation: 26362

It seems that the ChargeDate is not a related entity. Check Retaled entities documentation to see the purpose of include

Seems like you either have not a relationship between the types and that you want to use something like the select property to get new objects based on the data already retrieved from the query.

Upvotes: 1

Related Questions