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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:05:55+00:00 2026-06-13T06:05:55+00:00

Hi I have a linq query where I compare an object with all other

  • 0

Hi I have a linq query where I compare an object with all other entity stored in my database. Firstname and lastname are mandatory fields so I don’t have to check for null. But in the case of street I have to do these checks.

I want to match if both fields are null or empty strings or that they are the same. The below linq query is working fine.

But I was wandering isn’t there any way to make it more readable. For example with a custom function like Bool FieldsAreEqualOrBothNullOrEmpty(r.street, request.street)

Can’t figure how to do that and if it’s possible.

var same = 
   from r in db.Requests
   where r.firstName.ToLower() == request.firstName.ToLower()
      && r.lastName.ToLower() == request.lastName.ToLower()

      //Seems long to just compare two fields
      && ( string.IsNullOrEmpty(r.street) && string.IsNullOrEmpty(request.street) ) 
         || r.street.ToLower() == request.street.ToLower()

      select r;
return same;
  • 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-13T06:05:57+00:00Added an answer on June 13, 2026 at 6:05 am

    I would simplify:

    var same = 
       from r in db.Requests
       where r.firstName.ToLower() == request.firstName.ToLower()
          && r.lastName.ToLower() == request.lastName.ToLower()
       select r;
    
    if(string.IsNullOrEmpty(request.street)) {
        same = same.Where(r => string.IsNullOrEmpty(r.street));
    } else {
        string street = request.street.ToLower();
        same = same.Where(r => r.street.ToLower() == street);
    }
    

    The nice thing about the this is that it keeps the query simple in each case.
    You could use similar logic for the first two if you choose, and it could also be moved to an expression-based utility method. Untested:

    public static IQueryable<T> FieldsAreEqualOrBothNullOrEmpty<T>(
        this IQueryable<T> source,
        Expression<Func<T, string>> member, string value)
    {
        Expression body;
        if (string.IsNullOrEmpty(value))
        {
            body = Expression.Call(
                typeof(string), "IsNullOrEmpty", null, member.Body);
        }
        else
        {
            body = Expression.Equal(
                Expression.Call(member.Body, "ToLower", null),
                Expression.Constant(value.ToLower(), typeof(string)));
        }
    
        return source.Where(Expression.Lambda<Func<T,bool>>(
            body, member.Parameters));
    }
    

    then:

    var same = db.Requests
               .FieldsAreEqualOrBothNullOrEmpty(x => x.firstName, request.firstName)
               .FieldsAreEqualOrBothNullOrEmpty(x => x.lastName, request.lastName)
               .FieldsAreEqualOrBothNullOrEmpty(x => x.street, request.street);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to compare three dates in linq query (datetime a < datetime b
I have a LINQ query that returns some object like this... var query =
I have a Linq query that returns an object of the type IQueryable<ClassX> .
I have a Linq to Entity query that compares dates. I'm querying a SQL
I have this LINQ query which returns the indexes of all the items in
I have a Linq query that looks something like this: var myPosse = from
I have this linq query that works well (although it may be written better,
I have a LINQ query to a DataTable : var list = from row
I have a Linq query that looks something like this: var query = from
I have a linq query. I have a bunch of parameters from a form

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.