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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:43:18+00:00 2026-06-04T11:43:18+00:00

I am building a search feature in an asp.net application and I am using

  • 0

I am building a search feature in an asp.net application and I am using LINQ to SQL to retrieve data based on the selected search criteria. The search criteria are

  1. Country
  2. City
  3. District
  4. number of rooms
  5. Rent Cycle

Only the first search criterion, Country, is the mandatory field. However, if the user entered values for criteria 2, 3, 4 and/or 5 then the entered values should be taken into account and only retrieve results that matched all entered search criterion. Notice that if one of the criterion 2, 3, 4 and/or 5 is left empty (null) then LINQ should act as ‘DONT CARE’ and return that row.

So for example, if the criteria entered are:

  1. Country = USA
  2. City = null
  3. District = null
  4. number of rooms = null
  5. Rent Cycle = null

Then all rows with Country == USA should be returned.

Another example:

  1. Country = UK
  2. City = null
  3. District = null
  4. number of rooms = 5
  5. Rent Cycle = null

Then all rows with Country == UK and NumberOfRooms == 5 should be returned.

How do I achieve this in LINQ to SQL?

Here is what i have so far:

var data = from x in db.Units where x.Country == coutnryID && /*PLEASE HELP!*/ select x;
  • 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-04T11:43:19+00:00Added an answer on June 4, 2026 at 11:43 am

    Try this (assuming cityId, districtId, rooms and rentCycle are the variables that you’re wanting to search on:

    var data = from x in db.Units
        where x.Country == countryId
            && (cityId == null || x.City == cityId)
            && (districtId == null || x.District == districtId)
            && (rooms == null || x.Rooms == rooms)
            && (rentCycle == null || x.RentCycle == rentCycle)
        select x; 
    

    I’m basically saying, if your variables you want to search on are null, then disregard them, otherwise match them to the corresponding field in the Unit.

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

Sidebar

Related Questions

I'm building an ASP.NET MVC site that uses LINQ to SQL. In my search
I'm building a search engine for documents using asp.net mvc. The results of the
I am building a search page in asp.net 3.5. Search takes a bit of
I'm building a windows application that search items from sharepoint document libraries, using the
I'm building a search feature for my Symfony2 project, and I wrote the SQL
At the moment I am building a big project using: ASP.Net MVC 4 jQuery
I'm building an ASP.NET MVC application and I'm trying to deploy it on a
I am building a search view in asp.net MVC 2 So I have: public
I'm building a prototype search application using CakePHP and I have a search controller
I'm building a small website using ASP.net/C# and I wanted to know how to

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.