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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:45:51+00:00 2026-05-27T15:45:51+00:00

Is there an easier way to write the expression trees with Predicate Builder. It

  • 0

Is there an easier way to write the expression trees with Predicate Builder. It just seems like a lot of code that can be condensed.

Expression<Func<EventGymCourt, object>> gymCourt = q => q.GymCourt;
Expression<Func<EventGymCourt, object>> gym = q => q.GymCourt.Gym;
Expression<Func<EventGymCourt, object>> address = q => q.GymCourt.Gym.Address;

_eventGymCourtRepository.GetWithStart(page, pageSize, new[] { gymCourt, gym, address }....
  • 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-27T15:45:52+00:00Added an answer on May 27, 2026 at 3:45 pm

    Well, I doubt that the types of the GymCourt, Gym and Address members are all object, so it’s going to be difficult to get type-inference to work in your favour.

    One way would be to use an array-initializer:

    Expression<Func<EventGymCourt, object>>[] exprs 
          = { q => q.GymCourt, q => q.GymCourt.Gym, q => q.GymCourt.Gym.Address };
    
    _eventGymCourtRepository.GetWithStart(page, pageSize, exprs);
    

    If you really need each expression in a separate variable, you can use a using-alias to shorten the variable type-declaration.

    // At the top of your file....
    using CourtObjectExpr = 
      System.Linq.Expressions.Expression<System.Func<MyNameSpace.EventGymCourt, object>>;
    
    ...
    
    CourtObjectExpr gymCourt = q => q.GymCourt;
    CourtObjectExpr gym = q => q.GymCourt.Gym;
    CourtObjectExpr address = q => q.GymCourt.Gym.Address;
    

    By the way, you appear to be searching for the Linq.Expr method from LINQKit (the library that contains PredicateBuilder). This uses the compiler’s type-inference features to condense the conversion of a lambda to an expression-tree in typical scenarios. I don’t think this is all that useful in your example since you require the expression’s return-type to be object rather than the property-type.

    But assuming you want to create expressions with the property-type as the expression’s return-type, you could have done (quite succinctly):

    // Not directly applicable to your scenario (you can add a cast).
    var gymCourt = Linq.Expr((EventGymCourt q) => q.GymCourt);
    var gym =  Linq.Expr((EventGymCourt q) => q.GymCourt.Gym);
    var address = Linq.Expr((EventGymCourt q) => q.GymCourt.Gym.Address);
    

    This lets compiler type-inference work in your favour; the source-code for the method is as simple as:

    public static Expression<Func<T, TResult>> Expr<T, TResult>
               (Expression<Func<T, TResult>> expr)
    {
        return expr;
    }
    

    While that’s not a direct fit for your situation, it’s good to have in your toolkit.

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

Sidebar

Related Questions

Is there an easier way to step through the code than to start the
Is there an easier way of cleaning up a database that has a ton
I get this error: Can't locate Foo.pm in @INC Is there an easier way
Is there an easier way to write this? I know if i didnt need
Is there an easier way to do something like the following in Core Data:
Is there an easier way to write the integer value for enum flags without
I currently have a selector that looks like this: $(span[row!='2'][row!='5'][row!='1']); Is there an easier
Is there an easier way to achieve the following? var obj = from row
Is there an easier way to prevent a duplicate insert after refresh? The way
IS there an easier way to animate the table view. All the rows in

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.