Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4122168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:26:49+00:00 2026-05-20T23:26:49+00:00

My Vehicle type: public class Vehicle : EntityObject { private Lazy<string> _nameFromDelegate = null;

  • 0

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 in certain mappings as delegates so they’re evaluated in memory rather than L2S trying to translate them to SQL.

In the example below i’m trying to map the “vehicle.Name” value from SQL onto two properties on my Vehicle type: the “Name” string property and the “NameFromDelegate” string property (which encapsulates the Lazy<string>).

I’m hoping to prove that it doesn’t make any difference to L2S whether i pass in the “vehicle.Name” to the string ctor param or to the Lazy<string> ctor param. But perhaps it does:
enter image description here

I don’t understand why there is a need to cast 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:26:49+00:00Added an answer on May 20, 2026 at 11:26 pm

    This appears to work 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 can’t do this:

    var r = from veh in ctx.vehicles
                        select new Func<int>(() => veh.id);
    r.ToList();
    

    It appears that L2S is treating the lambda expression creation as a value assignment, instead as a method call and is trying to convert the value from the db to it. I’m saying it’s a bug since this works on linq to objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public abstract class Vehicle { protected void SomeMethod<T>(String paramName, ref T myParam, T val)
I have the following types: public abstract class Vehicle { public int Id {
Given a Vehicle class and a VehicleProperty class... public class Vehicle { public virtual
I have a method like: public virtual IList<string> Validate() { ... } I want
Consider the following code : public class Main implements Vehicle, Car { public static
I have a vector with pointers of type Vehicle. Vehicle is the base class
I have this problem: The Vehicle type derives from the EntityObject type which has
I have a vehicle entity in my data model. It has name, make, model,
Assume a class Vehicle has a method called StartEngine, and in my subclass called
I have an Vehicle class. From Vehicle, I extend Car class (and some others

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.