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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:53:47+00:00 2026-06-15T23:53:47+00:00

I have a repository where several methods are using the same piece of logic

  • 0

I have a repository where several methods are using the same piece of logic within the predicate.

public IList<Loan> GetLoansByCommitmentID(int commitmentID)
{

    var query = this.context.Loans.Where(l => l.CommitmentLoan != null &&
                                         l.CommitmentLoan.CommitmentID == commitmentID && 
                                         (l.LoanStatusTypes == null || (l.LoanStatusTypes.Description != "Invalid")));

    return query.ToList();
}

In the code above, it’s the last parenthesized subexpression:

(l.loanStatusTypes == null || (l.LoanStatusTypes.Description != "Invalid"))

I’d like to move this piece into a private method of the repository so that it takes a Loan, in this case, and evaluates to a boolean. If I move this logic into a method, however, EF does not understand how to evaluate the method call. So after thinking on it a bit, I decided perhaps the right way to do this would be for the private repository method to take a Loan as a parameter then return an Expression to be used in the calling lambda expression, kind of like an Expression factory method i.e.:

public Expression IsLoanInvalid(Loan l);

Does anyone know if this will overcome the inability of EF to understand the method call or should I instead be creating a custom ExpressionVisitor or is there another solution I should be trying?

Also, if the above proposed solution is possible and I go this route, how can I build an expression tree so that it uses the parameter passed in? I’ve successfully implemented the a method that builds the expression but have been unsuccessful thus far in passing the Loan parameter to the Expression I am building to be returned to the calling code. Thank you in advance.

  • 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-15T23:53:48+00:00Added an answer on June 15, 2026 at 11:53 pm

    You could create a variable to hold that piece of your where predicate.

    public Expression<Func<Loan, bool>> LoanStatus = loan => loan.LoanStatusTypes == null || loan.LoanStatusTypes.Description != "Invalid";
    

    And then add a second where off your IQueriable

    public IList<Loan> GetLoansByCommitmentID(int commitmentID)
    {    
        var query = this.context.Loans
             .Where(l => l.CommitmentLoan != null && l.CommitmentLoan.CommitmentID == commitmentID)
             .Where(LoanStatus);
    
        return query.ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a public Sonatype Nexus Maven Repository which is a proxy for several
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
I have a Spring based webapp. I am using several repository classes with annotations
Suppose I have a git repository with several branches. I suspect some of the
I'm using Visual SVN on my Windows Box. I have Repository Application , which
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I have one 'super' repository in GitHub which will contain several applications I would
I have some trouble with a git repository that contains several submodules. The super
Often I want to have a main repository of source, shared by several similar
I have a git repository that I am using and I've screwed up. I

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.