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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:33:27+00:00 2026-06-02T05:33:27+00:00

i am trying to design a LINQ query that filters on a specific location.

  • 0

i am trying to design a LINQ query that filters on a specific location. In my UI dropdown list, that dropdown has the first entry as ‘All Sites’. So when the query see’s ‘All Sites’ as the value, it should not perform the filter. This may not make much sense but this is how i used to do it in SQL..

select * from table
where ((location = @loc and @loc is not null) or (@loc is null))

this is how i am trying to do it in LINQ.

var dataSetB = db.Data
.Where(j => loc != 'All Sites' && j.Location1.Description.Equals(loc));

The problem is that when the user selects ‘All Sites’ – no result are returned when i would be expecting all results not filtered by location.

Does anyone know how to accomplish this in one LINQ statement?

example dropdown..

<All Sites>
<Sydney>
<New York>
<London>
  • 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-02T05:33:28+00:00Added an answer on June 2, 2026 at 5:33 am

    Your dropdown wouldn’t normally have just text, it will normally be bound to text/value pairs. So your text entry that reads New South Wales would have a corresponding value of NSW. The entry that reads All Sites would have a value of null. So you could change your query to:

    public object GetMyStuff(string loc)
    {
        var dataSetB = db.Data
                         .Where(j => loc == null || j.Location1.Description == loc);
    }
    

    Logical shortcutting applies in the expression, so the j.Location1.Description will only be compared if the passed loc value is not null.

    Using neutral location values is important – you don’t really want to be comparing the literal text All Sites for a bunch of reasons.

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

Sidebar

Related Questions

I am trying to design a dropdown menu that covers the entire width of
I am trying to design model associations in rails that consist of the following
I am trying to figure out how to go about writing a linq query
I am trying to design a layout so that I can change the views
I'm trying to convert a standard SQL query to a LINQ to SQL query.
I see that the Quick Watch window has access to all properties, irrespective of
I'm trying to design a calendar-type table that is built dynamically based on data
I'm trying to design a data-structure around a stack that blocks until the stack
I'm trying to design application that will have UI with database in the backend.
Without getting into all of the gory details, I am trying to design a

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.