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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:26:10+00:00 2026-05-30T21:26:10+00:00

I am new to lambda expressions and I am having issues trying to figure

  • 0

I am new to lambda expressions and I am having issues trying to figure out how to represent my entity framework query in one. I also could be wrong and there is just a better way to do what I want. If this is true please let me know. My scenario is I have an advanced search screen where you could choose to search bu name, customer number, or phone number. You can search by more than 1 if you want. I am using Entity Framework as my backend and have repositories set up for my tables. Below is the code I am trying to use

Func<Parties, bool> exp;

exp = null;

if (vm.CustomerNumberCriteria != null)
{
    custID = Convert.ToInt32(vm.CustomerNumberCriteria);
    exp = o => o.ID == custID;
}


if (vm.NameCriteria != null)

    exp += o => o.LastName.Contains(vm.NameCriteria) || o.FirstName.Contains(vm.NameCriteria) || o.MiddleName.Contains(vm.NameCriteria) || o.Designation.Contains(vm.NameCriteria);

if (vm.PhoneNumberCriteria != null)

    exp += o => o.CentralPhoneNumbers.Any(child => child.PhoneNumber == vm.PhoneNumberCriteria);

//TODO set tempresults
tempresults = custs.All.Where(exp).ToList();

My issue is it seems to treat this like an and and I need results if there is a match on any of the search criteria.

Thanks

  • 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-30T21:26:12+00:00Added an answer on May 30, 2026 at 9:26 pm

    There are two big problems here. Firstly, you’re using delegates, not expression trees – which means your entire table is going to be pulled back to the client and filtered there. You want Expression<Func<Parties, bool>>.

    Now, if you want to build up “or” expression trees, the simplest approach is to use PredicateBuilder:

    var predicate = PredicateBuilder.False<Parties>();
    
    if (vm.CustomerNumberCriteria != null)
    {
        custID = Convert.ToInt32(vm.CustomerNumberCriteria);
        predicate = predicate.Or(o => o.ID == custID);
    }
    
    if (vm.NameCriteria != null)
    {
        custID = Convert.ToInt32(vm.CustomerNumberCriteria);
        predicate = predicate.Or(o => o.LastName.Contains(vm.NameCriteria) /* etc */)
    }
    
    if (vm.PhoneNumberCriteria != null)
    {
        predicate = predicate.Or(o => o.CentralPhoneNumbers.Any
                         (child => child.PhoneNumber == vm.PhoneNumberCriteria));
    }
    
    tempresults = custs.All.Where(predicate).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new with lambda expressions and linq and can't figure out what I'm doing
I'm very new to lambda expressions in C#, and I'm having trouble conceptualizing how
I am new to C# and trying to understand lambda expressions along with delegates.
I'm trying to use Rhinomocks 3.5 and the new lambda notation to mock some
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
Is it possible to use the new lambda expressions in Visual C++ 2010 as
How can I combine two lambda expressions into one using an OR ? I
I'm new to lambda expressions and looking to leverage the syntax to set the
Probably something simple, but as I'm new to lambda expressions, the problem evades me:
I'm still having problems to write lambda expressions that should create some object and

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.