Expression cannot contain lambda expressions

Khadim Ali picture Khadim Ali · May 5, 2014 · Viewed 49k times · Source

I have fetched the List<> object as below (with .Include()):

List<vDetail> entityvDetails =
    context.vDetails
    .Include("payInstallment.appsDetail")
    .Include("payInstallment.appsDetail.application")
    .Include("payInstallment.appsDetail.purposes")
    .Where(e => e.vch_id == 123).ToList();

And then somewhere in the code ahead I tried to filter the entity record as below:

foreach (vDetail item in lstVDetails)
{
    ... 

    int purposeId = entityvDetails.Where(e => e.sad_id == item.sad_id).FirstOrDefault().payInstallment.appsDetail.purposes.prp_id;

    ...
}

Code compiling perfect. However, the runtime returning following error (although all navigations are included):

Object reference not set to an instance of an object.

So I set for debugging using the watch window. Now while analyzing the below statement in watch window:

entityVoucherDetails.Where(e => e.sad_id == item.sad_id).FirstOrDefault()

the watch window generated following error:

Expression cannot contain lambda expressions.

Please if anybody can tell me what could be the reason?

Answer

BlueM picture BlueM · May 5, 2014

Evaluating Lambda expressions in debugger (watch window) is not supported yet.

Theres an open feature request for it.

For debugging your problem you should assign the result of the lambda expression to a dedicated variable and use that in following statements.

var entity = entityvDetails.Where(e => e.sad_id == item.sad_id).FirstOrDefault();

Update 08/2014: Microsoft posted an update on the feature request announcing that they started to work on it:

So here’s where we stand.

  • 1) We want this to work as much as you do. It’s not under review – it’s in progress.
  • 2) We figured out how to make it work; it simply requires rewriting everything.
  • 3) We’re rewriting everything.
  • 4) Rewriting everything takes a lot of time and a lot of testing.

Update 11/2014: Microsoft finally implemented it with some limitations in VS2015. Read here.