Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC checkboxes

Posted on 2013-06-23
4
Medium Priority
?
441 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 1000 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 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 64

Expert Comment

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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

876 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