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.

3
  • 2
    It should works. 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 Commented Mar 17, 2016 at 9: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. Commented Mar 17, 2016 at 9:45
  • 1
    coffee time for me then. Commented Mar 17, 2016 at 9:53

2 Answers 2

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 :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Nice find, it's probably a bug then indeed.
Added as issue to Github for further research by the team: github.com/dotnet/roslyn/issues/9853
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.

2 Comments

When I use value it works indeed. I just don't get why I can do it without in code, but not in the immediate window
This seems like a bug in the expression evaluator because this should not give different results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.