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

  • SEARCH
  • Home
  • 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 8686763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:56:47+00:00 2026-06-12T22:56:47+00:00

I have a class represent a generic filter object public class Filter { public

  • 0

I have a class represent a generic filter object

public class Filter
{
    public string column { get; set; }
    public string operator { get; set; }
    public string data { get; set; }
}

that I am able to transform in a LambdaExpression thanks to code like this

public LambdaExpression ToExpression( IQueryable query ) {
    LambdaExpression toReturn = null;
    ParameterExpression parameter = Expression.Parameter( query.ElementType, "p" );
    MemberExpression memberAccess = GetMemberExpression( column, parameter );
    ConstantExpression filter = Expression.Constant( Convert.ChangeType( data, memberAccess.Type ) );
    WhereOperation condition = (WhereOperation)StringEnum.Parse( typeof( WhereOperation ), operator );
    LambdaExpression lambda = BuildLambdaExpression( memberAccess, filter, parameter, condition, data );
    if ( toReturn == null ) {
        toReturn = lambda;
    }
    return toReturn;
}

As per the requirement the Filter member column can contains a sort of navigation sintax for querying fields that is expressed like FieldA.FieldB.Description that translates to:

  • Get the value of type T returned from the FieldA property
  • Get the value of type T1 returned from the FieldB of T
  • Get the value of type T2 returned from the Description of T2

The result is a lambda like this one: p.FieldA.FieldB.Description == "data"
and I can use this result as a parameter of the Where extension method of any IQueryable

The problem comes out when one of the members of the navigation query is a Nullable type. In that case, supposing FieldA is nullable, the correct lambda should be

p.FieldA != null && p.FieldA.FieldB.Description == "data"

I have tried to implement this check while constructing the MemberExpression object using code like this one

MemberExpression memberAccess = null;
foreach ( var property in column.Split( '.' ) ) {
    memberAccess = MemberExpression.Property( memberAccess ?? ( p as Expression ), property );
    Type memberType = memberAccess.Type;
    if ( memberType.IsGenericType && 
         memberType.GetGenericTypeDefinition() == typeof( Nullable<> ) ) {

        //Create here an expression of type : memberAccess != null
    }
}

that is good for primitive types but does not work, for example, with other object references, like EF EntityReference object instances.
I know I could simply add another condition to the previous if like

if ( ( memberType.IsGenericType && 
       memberType.GetGenericTypeDefinition() == typeof( Nullable<> ) ) ||
       memberType.IsClass ) {
}

but that seems to me too much generic producing a lambda with too many condition and most of them maybe unuseful.

Is there any way to better individuate nullable references?

  • 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-06-12T22:56:48+00:00Added an answer on June 12, 2026 at 10:56 pm

    You can make the null check unconditional. Checking a value type for null is not illegal.

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

Sidebar

Related Questions

I have got a designing dilema. I have a class named UserAccount that represent
I have a ListItem class that is used to represent menu items in my
Id like to represent a Class object as JSON. For example, if I have
I have a set of classes that represent the storage of a font format
So I have a class that is a generic and it may need to,
I have a generic class which represents a fragment of text. That fragment of
I have a generic class that represents a document that can be of currently
I have data that I would ideally want to represent as so: LinkedList<T>[] However,
I have a collection of this object : public Class MyObject() { public int
I have a sorted mutable array of a class called Topic. The topics represent

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.