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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:22:13+00:00 2026-05-28T01:22:13+00:00

I am trying to create a generic Exists() Command class that is able to

  • 0

I am trying to create a generic Exists() Command class that is able to construct a where clause for ANY combination of public properties (fields) of ANY LinqToSQL class (tables) in my datacontext (database).

If have written the following based on snippets from MSDN, and am finding that I cannot successfully run the code unless I explicitly declare the entity type (in this case, MyApp.Data.Organization) of the Expression.Lambda parameter of the Expression.Call statement.

In other words,

Expression.Lambda<Func<tblType,bool>>(predBody, new ParameterExpression[] { pe }));

does not work, but,

Expression.Lambda<Func<MyApp.Data.Organization,bool>>(predBody, new ParameterExpression[] { pe }));

does work. I want something like the former so the method remains generic to all LinqToSQL classes in my data context.

I have verified that all the code prior to the Expression.Call statement works fine and will generate a correct predicate. What do I need to change in order to keep the parameter Type generic so that the same code will work for any LinqToSQL class in the data context?

Revision for Clarity
It seems my use of T as a variable was confusing things. Here is revised code:

OrganizationCriteria criteria = new OrganizationCriteria { OrganizationId = 2 };
 using (var ctx = ContextManager<MyApp.Data.MyAppDataContext>.GetManager(myConnectionString, false)) {
                IQueryable tbl = ctx.DataContext.GetTable(criteria.EntityType).AsQueryable();
                Type tblType = tbl.ElementType;

                ParameterExpression pe = Expression.Parameter( tblType, "Item" );

                Expression left;
                Expression right;
                Expression prev = null;
                Expression curr = null;
                Expression predBody = null;
                foreach ( KeyValuePair<string, object> kvp in criteria.StateBag ) {
                    prev = curr;
                    object val = kvp.Value;
                    if (val is System.String ) {
                        left = Expression.Call( pe, typeof( string ).GetMethod( "ToLower", System.Type.EmptyTypes ) );
                        right = Expression.Constant( kvp.Value.ToString() );
                    }
                    else {
                        left = Expression.Property( pe, tblType.GetProperty( kvp.Key ) );
                        right = Expression.Constant( kvp.Value, tblType.GetProperty( kvp.Key ).PropertyType );
                    }
                    curr = Expression.Equal( left, right );

                    if ( prev != null ) {
                        predBody = Expression.AndAlso( prev, curr );
                    }
                    else {
                        predBody = curr;
                    }
                }

                MethodCallExpression whereCall = Expression.Call(
                    typeof( Queryable ),
                    "Where",
                    new Type[] { tblType },
                    tbl.Expression,
                    Expression.Lambda<Func<tblType,bool>>(predBody, new ParameterExpression[] { pe }));

                var results = tbl.Provider.CreateQuery( whereCall );
}
  • 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-28T01:22:14+00:00Added an answer on May 28, 2026 at 1:22 am

    The answer is to use the non-generic overload of Expression.Lambda(). Thus, replacing:

    Expression.Lambda<Func<tblType,bool>>(predBody, new ParameterExpression[] { pe })
    

    with

    Expression.Lambda(predBody, new ParameterExpression[] { pe })
    

    did the trick, and I now have a totally generic method for querying any table on any fields.

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

Sidebar

Related Questions

I'm trying to create a generic class in PHP that will provide a way
I am trying to create a generic Identifier class which I would be able
I am trying to create a generic formatter/parser combination. Example scenario: I have a
I am trying to create a generic class which new's up an instance of
I was trying to create a generic Dictionary that implements IXmlSerializable (credit to Charles
I am trying to create a generic method that will read an attribute on
I'm trying to create an instance of a generic class using a Type object.
I am trying to create a generic class to write and read Objects to/from
I am trying to create a generic extension that uses 'TryParse' to check if
I'm trying to create a generic list from a specific Type that is retrieved

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.