How can I pass Lazy <T> to my projection?
My Vehicle Type:
public class Vehicle : EntityObject { private Lazy<string> _nameFromDelegate = null; private Lazy<IList<Component>> _components = null; public Vehicle(int id, string name, Lazy<string> nameFromDelegate, Lazy<IList<Component>> components) : base(id) { this.Name = name; this._nameFromDelegate = nameFromDelegate; } public string Name { get; private set; } public string NameFromDelegate { get { return this._nameFromDelegate.Value; } } public IList<Component> Components { get { return this._components.Value; } } } I want to project my type in L2S using the constructor and pass certain mappings as delegates so that they are evaluated in memory, not the L2S that tried to translate them into SQL.
In the example below, I am trying to map the value of "vehicle.Name" from SQL to two properties in my Vehicle type: the Name property string and the NameFromDelegate string property that encapsulates Lazy<string> ).
I hope to prove that L2S does not make any difference whether I pass string ctor param or Lazy<string> ctor param to "vehicle.Name". But perhaps this is so: 
I do not understand why it is necessary to drop from string to Func<string> . Ideas?
Stack trace for reference:
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Data.Linq.DBConvert.ChangeType(Object value, Type type) at Read_Vehicle(ObjectMaterializer`1 ) at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at DelegateQueries.Models.VehicleRepoWithDelegates.GetAll() in %path%\DelegateQueries\Models\VehicleRepoWithDelegates.cs:line 26 at DelegateQueries.Tests.RepoTests.VehicleRepo_CanReturn_NameFromDelegateProp_InLinq_WithDelegate() in %path%\DelegateQueries\DelegateQueries.Tests\RepoTests.cs:line 31 There seems to be a problem around the problem:
static Lazy<T> Lazyfy<T>(T input) { return new Lazy<T>(() => input); } public IQueryable<Vehicle> Vehicles() { return from veh in ctx.vehicles select new Vehicle(veh.id, veh.name, Lazyfy(veh.name), null); } UPDATE , you can also do this:
static Func<T> Functify<T>(T input) { return () => input; } public IQueryable<Vehicle> Vehicles() { return from veh in ctx.vehicles select new Vehicle(veh.id, veh.name, new Lazy<string>(Functify(veh.name)), null); } but you cannot do this:
var r = from veh in ctx.vehicles select new Func<int>(() => veh.id); r.ToList(); It looks like L2S treats the creation of a lambda expression as a value assignment, instead as a method call, and tries to convert the value from db to it. I am talking about an error since it works on linq for objects.