Solved

MVC checkboxes

Posted on 2013-06-23
4
438 Views
Last Modified: 2013-06-23
Hi,

I have 5 checkboxes for different Job types.  

the user can select multiple checkboxes.

my linq filter is wrong.

how can I say if the user select one or more of the checkboxes to filter it.

here's what I have

    if (Contract)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 2);
                }

                if (Intership)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 3);
                }

                if (NonProfit)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 5);
                }

                if (PartTime)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 4);
                }

                if (Telecommute)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 1);
                }

right now if I select more than one selection it will not display anything.

Thanks,
0
Comment
Question by:lulu50
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 39269287
You filter first on one id, then you filter the filtered resultset on a second id... That's like doing a logical AND on two exclusive states...  This is why you get no results.  You want something along these lines:
                List<int> filterList = new List<int>();
                if (Contract)
                {
                    filterList.Add(2);
                }

                if (Intership)
                {
                    filterList.Add(3);
                }

                if (NonProfit)
                {
                    filterList.Add(5);
                }

                if (PartTime)
                {
                    filterList.Add(4);
                }

                if (Telecommute)
                {
                    filterList.Add(1);
                }
                ResultSet = ResultSet.Where(c => c.a.JobTylesID.HasValue && filterList.Contains(c.a.JobTypesID.Value));

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 39269341
Hi lulu50;

I think something like this will give you the solution you are looking for. The main reason why you solution was not giving the wanted results is that doing multiple where clauses like you were doing is a AND operation and not OR.

List<int> jobType = new List<int>();

if (Contract) jobType.Add(2);
if (Intership) jobType.Add(3);
if (NonProfit) jobType.Add(5);
if (PartTime) jobType.Add(4);
if (Telecommute) jobType.Add(1);

ResultSet = "The first part of the query HERE".Where(c => jobType.Contains(c.a.JobTypesID.Value));

Open in new window

0
 

Author Closing Comment

by:lulu50
ID: 39269460
Thank you
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39269631
Not a problem lulu50, glad to help. ;=)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question