1

Disclaimer: I already checked this question but this is about the Immediate window.

I'm using Entity Framework to fetch some results, so I have a list of logging:

var result = ctx.Logging.Where(*filtering*).ToList();

But I want to know the distinct Datatypes of the list (an enumeration, underlying type byte and Datatype is nullable, if I make it not nullable it works for some reason). When I try to do it in the immediate window:

result.Select(r => r.Datatype).Distinct().ToList();

I get the message:

Internal error in the expression evaluator.

However it works fine when I do it in code, for example var test = result.Select(r => r.Datatype).Distinct().ToList();. I already tried using 'managed compatibility mode' and 'legacy expressions' from the debugging options but then I get another message:

Expression cannot contain lambda expressions

Am I missing something or is this a bug in Visual studio 2015?

Minimal, Complete, and Verifiable example:

class Program
{
    static void Main(string[] args)
    {
        List<Test> tests = new List<Test>();
        for(int i = 0; i<100; i++)
        {
            if (i % 2 == 0)
                tests.Add(new Test { ID = i, Enum = TestEnum.Value1 });
            else
                tests.Add(new Test { ID = i, Enum = TestEnum.Value2 });
        }
        var distinct = tests.Select(t => t.Enum).Distinct().ToList();
    }
}

public enum TestEnum : byte
{
    Value1 = 1,
    Value2 = 2
}
public class Test
{
    public int ID { get; set; }
    public TestEnum? Enum { get; set; }
}

In code tests.Select(t => t.Enum).Distinct().ToList(); works, in immediate window it doesn't.

Community
  • 1
  • 1
Alexander Derck
  • 13,818
  • 5
  • 54
  • 76
  • 2
    It [should works](https://blogs.msdn.microsoft.com/visualstudioalm/2014/11/12/support-for-debugging-lambda-expressions-with-visual-studio-2015/). Wait: ***NOTE**: Lambda expressions that require native functions to run (e.g. LINQ-to-SQL) are not supported.*. Which may turn your question to a duplicate of [this one](http://stackoverflow.com/questions/23470607/expression-cannot-contain-lambda-expressions) – Thomas Ayoub Mar 17 '16 at 09:43
  • @Thomas I only get the "cannot contain expression" when I turn on legacy mode. If I turn it off I get the "expression can't be evaluated". However if I make the enum non-nullable it works in immediate window. Seeing your edit: I call `ToList` so I'm not using Linq-to-entities or Linq-to-sql anymore, but Linq-to-object. – Alexander Derck Mar 17 '16 at 09:45
  • 1
    coffee time for me then. – Thomas Ayoub Mar 17 '16 at 09:53

2 Answers2

2

I think it is a bug in the expression evaluator as it seems to work with a workaround like this:

tests.Select(t => t.Enum).ToList().Distinct().ToList()

Here is the same result using this:

? tests.Select(t => t.Enum).ToList().Distinct().ToList()
Count = 2
    [0]: Value1
    [1]: Value2

I noticed that 'quickwatching' the expression in your code also results in the error:

Internal error in the expression evaluator.

At least it is consistent in that area :-)

Maarten van Stam
  • 1,901
  • 1
  • 11
  • 16
1

Your expression does not work:

tests.Select(t => t.Enum).Distinct().ToList();
'Internal error in the expression evaluator.'

But thats because Enum is a nullable type.

tests.Select(t => t.Enum.Value).Distinct().ToList();
    'Count = 2
        [0]: Value1
        [1]: Value2'

And in fact, native functions used in EF does not work with lambda expressions in the immediate window.

Kevin
  • 506
  • 6
  • 13