Entity Framework does not have translations to every .NET function, so we had to register some of our own.
The reason casting to AsEnumerable() worked is that a delegate call gets inserted that executes *before * the Select() call. So, EF will do a SELECT * FROM UD13 and fetch all the data – and all 100+ columns – before calling the Select() function, which then just re-materializes the data into the anonymous type.
Using ErpEFFunctions.ConvertToInt allows EF to make the T-Sql query match the structure of the anonymous type – with just four columns appearing in the SQL SELECT statement. It’s way more efficient.
BTW, the ErpEFFunctions.ConvertToInt function basically does this…
return Int32.TryParse(val, out Int32 result) ? result : default;
… I’m not sure why we didn’t call Convert.ToInt32() under the hood. Oh, and I wish the actual code was this compact :).