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 3976510
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:48:47+00:00 2026-05-20T04:48:47+00:00

From this question I asked 5 minutes ago, it’s clear that the following code

  • 0

From this question I asked 5 minutes ago, it’s clear that the following code throws an exception, stating that

Unhandled Exception:
System.InvalidOperationException: The
binary operator Equal is not defined
for the types
‘System.Nullable`1[System.Int32]’ and
‘System.Int32’.

Code

public static void GetResultCollection<T>() {
        AccrualTrackingEntities db = new AccrualTrackingEntities();
        var result = db.CreateQuery<T>(String.Format("[{0}]", typeof(T).Name + "s"));

        int? ItemTypeValue = 1;

        var param = Expression.Parameter(typeof(T));

        var lambda = Expression.Lambda<Func<T, bool>>(
            Expression.Equal(
                Expression.Property(param, "ProcInstId"),
                Expression.Constant(ItemTypeValue)),
            param);

        var list = result.Where(lambda).ToList();
    }

This code, however, with the type explicitly listed in Expression.Constant does work

class Program {
    public static void GetResultCollection<T>() {
        AccrualTrackingEntities db = new AccrualTrackingEntities();
        var result = db.CreateQuery<T>(String.Format("[{0}]", typeof(T).Name + "s"));

        int? ItemTypeValue = 1;

        var param = Expression.Parameter(typeof(T));

        var lambda = Expression.Lambda<Func<T, bool>>(
            Expression.Equal(
                Expression.Property(param, "ProcInstId"),
                Expression.Constant(ItemTypeValue, typeof(int?))),
            param);

        var list = result.Where(lambda).ToList();
    }

The question is, why is Expression.Constant not able to convert implicitly from int? to … int?

  • 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-20T04:48:48+00:00Added an answer on May 20, 2026 at 4:48 am

    Expression trees work at a lower level to normal source code – you can think of them as working at the level of the output of the compiler rather than the input. So while there’s an implicit conversion from int to int? in C#, that conversion has to be represented in IL whenever the compiler uses it for a normal method… so it also has to be present in an expression tree representation.

    Having said that, your example is somewhat unclear, given that you’re trying to use an int (namely ItemTypeValue.Value) as a value for an int? constant, and we don’t know what the type of the ItemType property is either.

    A short but complete example of what you’d expect to work would really help.

    EDIT: Okay, I think I’m with you now. The problem is that if you use

    int? foo = 1;
    Expression.Constant(foo);
    

    then that calls Expression.Constant(object) which boxes the value of foo. At that point, Expression.Constant can’t tell it was originally an int?, because it’s now a boxed int. That’s just the way .NET boxing works:

    int? foo = 1;
    object o = foo;
    Console.WriteLine(o.GetType()); // Prints System.Int32
    

    That overload of Expression.Constant determines the overall type of the expression from the value that it’s given – so it creates an int expression, whereas you really want an int? expression.

    In order to maintain the type information properly, you have to use the overload which allows you to specify the type:

    int? foo = 1;
    Expression.Constant(foo, typeof(int?));
    

    It’s still not entirely clear from your question which code works and which doesn’t, but hopefully that’ll help…

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

Sidebar

Related Questions

I realize that there was a similar question asked here , but this is
This is really a follow-up from this question , as I think I've solved
I know this question has been asked a lot, but not yet to my
I saw this question asked a million times and none of the answers looked
I know this question has been asked many times before but I can't find
So based on this question ( here ), of which I asked last week,
Before I ask my question, let me just say that I know very little
I'm trying to create one entity from 2 tables that are related not by
I'm making the switch from a centralized SCM system to GIT. OK, I'll admit
In a client server application a client is connected to server, now if client

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.