Why can't I project ToString() in VB?

前端 未结 3 1873
不知归路
不知归路 2020-12-11 00:25

If you try to compile the query below in Visual Basic .NET, it fails.

From x In {1, 2} Select x.ToString()

The error given by the compiler

3条回答
  •  孤城傲影
    2020-12-11 01:19

    While I can't offer details about the error, a simple workaround it:

    From x In {1, 2, 3} Let str = x.ToString() Select str
    

    For what it's worth, when you have the following line:

    Dim b = From num In {1, 2, 3} Select num.ToString("d")
    

    it is compiles and decompiles to (using Reflector, to C#):

    public class Class1
    {
        [CompilerGenerated]
        private static string _Lambda$__1(int num)
        {
            return num.ToString("d");
        }
    
        public void Test1()
        {
            IEnumerable b = new int[] { 1, 2, 3 }
                .Select(new Func(Class1._Lambda$__1));
        }
    }
    

    There doesn't seem to be any anonymous class along the way.

提交回复
热议问题