Solved

MVC checkboxes

Posted on 2013-06-23
4
431 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 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

15 Experts available now in Live!

Get 1:1 Help Now