Solved

MVC checkboxes

Posted on 2013-06-23
4
430 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
  • 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 62

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 62

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now