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

The Archive Base Latest Questions

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

I am trying to create Linq Expression that I can use to query child

  • 0

I am trying to create Linq Expression that I can use to query child entity for parent properties(PATIENT entity in this case), using this method:

private IQueryable<LAB_ORDER> SelectFilter(string fieldName, string value)
{
   var param = Expression.Parameter(typeof(PATIENT), "item");
   var field = Expression.PropertyOrField(param, fieldName);

   var search = Expression.Constant(value, typeof(string));

   var body = Expression.Call(field, "Contains", null, search);

   Expression<Func<LAB_ORDER, bool>> lambda;

   if (String.IsNullOrEmpty(value))
   {
      lambda = x => true;
   }
   else
   {
      lambda = Expression.Lambda<Func<LAB_ORDER, bool>>(body, parm);

   }

   var linqFilter = from e in context.LAB_ORDERS select e;

   return linqFilter.Where(lambda);
} 

Looks like it builds an expression correctly, but exception I am getting is this:

System.ArgumentException:
ParameterExpression of type ‘PATIENT’
cannot be used for delegate parameter
of type LAB_ORDER’.

While I can do linqFilter.Where( x => x.PATIENT.LAST_NAME == “Smith”) without any problems, can’t use expression above, which should translate to something similar to this. How would I fix the expression above to correct it? I am eagerly loading parent entity with a child so that’s not a problem.

EDIT

From Anders reply it looks like I need to modify lambda expression to:

lambda = Expression.Lambda<Func<PATIENT, bool>>(body, parm);

Now when I try to use it, linqFilter.Where(lambda) gives me compile error “No suitable overload” which I understand why – linqFilter is
"IQueryable<LAB_ORDER>", not "IQueryable<PATIENT>", so how should I do that?
Just to sum things up, the only question remains – how to create Expression for parent properties?

  • 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-13T21:09:28+00:00Added an answer on May 13, 2026 at 9:09 pm

    In the following statement you say that you will build a function which takes a LAB_ORDER argument and returns a bool.

    lambda = Expression.Lambda<Func<LAB_ORDER, bool>>(body, parm);
    

    However the parm variable contains a PATIENT. You will have to rewrite the expression lambda to take an LAB_ORDER instead. If I understand your intentions you correctly the expression you are trying to express is

    x => x.PATIENT.[fieldnName].Contains()
    

    but the expression you’ve built is

    x => x.[fieldName].Contains()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.