It really puzzles me. I know that LINQ-to-SQL processes the selection by processing the expression tree and trying to translate the material through the generated query, so some function translations do not work ( string.IsNullOrWhitespace is a regular annoyance).
I got into a situation in my code where LINQ-to-SQL was able to call my helper function via Select Select ... sometimes. In fact, it works when the method has one name, but does not work with it, it has a different name. I think this is best illustrated by the following program (it is about as simple as I could do this):
//
The database database has only one table named [Number] with one column [Value] INT NOT NULL .
As written, the program works for me, but uncommenting #define at the top will switch the name of the method call, and then the program will NotSupportedException when executing ToList() .
I tried several different method names to try to define a pattern, but could not. It looks like if a method is named that starts with To , it will throw a NotSupportedException , but it seems to work with any other name. This is still weird.
Can someone explain what is happening?
Here's another example without the #define switch, it just executes both methods back, and I still see the same problem. In particular, DoIt works, but ToValueType does not work.
using System; using Sandbox.Data; using System.Linq; namespace Sandbox.Console { class Program { static void Main(string[] args) { using (var dataContext = new SandboxDataContext()) { try { var myValueList = dataContext.Numbers.Select(x => new MyValue { Type = DoIt(x.Value), }).ToList(); System.Console.WriteLine("DoIt Succeeded, found {0} results", myValueList.Count); } catch (NotSupportedException) { System.Console.WriteLine("DoIt Failed, oh noes!"); } try { var myValueList = dataContext.Numbers.Select(x => new MyValue { Type = ToValueType(x.Value), }).ToList(); System.Console.WriteLine("ToValueType Succeeded, found {0} results", myValueList.Count); } catch (NotSupportedException) { System.Console.WriteLine("ToValueType Failed, oh noes!"); } System.Console.ReadKey(); } } public static MyValueType DoIt(int value) { return MyValueType.SpecialType; } public static MyValueType ToValueType(int value) { return MyValueType.SpecialType; } public sealed class MyValue { public MyValueType Type { get; set; } } public enum MyValueType { SpecialType, } } }