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

The Archive Base Latest Questions

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

How we can write a LINQ query for following select sql query: string brandid=1,2,3

  • 0

How we can write a LINQ query for following select sql query:

string brandid="1,2,3"
string bodystyleid="1,2,3"
-------------------
-----------------

select * from car
where brandid in (brandid)
and bodystyleid in (brandid)
----------------------
-------------------

My specific requirement is that if brandid or bodystyleid is blank(if user does not select
any checkbox of a particular search option) query should return all record for that particular where condition.

Please guide me.

Thanks,

Paul

  • 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-04T10:44:00+00:00Added an answer on June 4, 2026 at 10:44 am

    In order to fulfil your requirement about returning all items if none are specified, you need to check for the lists being empty.

    var brands = brandid.Split(',').Select(x => Int32.Parse(x));
    var styles = bodystyleid.Split(',').Select(x => Int32.Parse(x));
    
    var result = from c in car
                 where (!brands.Any() || brands.Contains(c.brandid))
                      && (!styles.Any() || styles.Contains(c.bodystyleid))
                 select c;
    

    (similar to sgmoore’s solution, but includes the check for no brand/style specified)

    I’ve not actually checked how this gets converted back to SQL – it may be more efficient to use a flag to indicate whether there are any values:

    var brands = ....;   // As above
    bool anyBrands = brands.Any()
    var result = from c in car
                 where (!anyBrands || brands.Contains(c.brandid))
                   .....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi How can I write the following sql query by linq Select * From
How can I make the following SQL a Linq query: SELECT * FROM ORDERS
How can I write a LINQ query that returns an hierachical anonymous type from
How can I write a query similar to this one in LINQ to SQL
I'm trying to write the following query in LINQ, but can't seem to get
I want to write a Linq to Sql query that does a count and
can anyone help me write a linq query, its a bit confusing... Basically i
Can I write the same queries (select, insert, update etc.) in LINQ to Entites
How does one select a specific object using query-expression style Linq? private static ObservableCollection<Branch>
hello, can you help me how to write Linq query (using Entity Framework) for

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.