Link to home
Create AccountLog in
Avatar of finance_teacher
finance_teacher

asked on

LINQ -- easy CASE statement ?

Currently below #1 works.

How can I change below #2 to be
simpler and work with "IN" statement
using ASP.net MVC4 C# ?
---------------------------------------------------------------------------------
#1
        public ActionResult Technician(string sabProjectID)
        {
            var myPOs =
            (from p in db.MAINT_WORK_REQ
             where
              p.WorkFlowStage == 6 && p.ProjectID == sabProjectID
             select p
            )
            ;
            return View(myPOs);
        }

---------------------------------------------------------------------------------
#2
        public ActionResult DisplayResults(string sabProjectID, string URLRole)
        {
            var myPOs =
              case
                WHEN URLRole='Inspector' THEN
                 (from p in db.MAINT_WORK_REQ
                  where
                   p.WorkFlowStage == 2 && p.ProjectID == sabProjectID
                  select p
                 )

               case
                WHEN URLRole='Lead' THEN
                 (from p in db.MAINT_WORK_REQ
                  where
                   p.WorkFlowStage in (6,7) && p.ProjectID == sabProjectID
                  select p
                 )

               case
                WHEN URLRole='Customer' THEN
                 (from p in db.MAINT_WORK_REQ
                  where
                   p.WorkFlowStage == 4 && p.ProjectID == sabProjectID
                  select p
                 )

            ;
            return View(myPOs);
        }
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi,

I think that the following will do exactly what you want:

public ActionResult DisplayResults(string sabProjectID, string URLRole)
{
    var myPOs = db.MAINT_WORK_REQ
                         .Where
                         (
                             x=>
                                 x.ProjectID == sabProjectID 
                                 && "2,4,6,7".Split(',').Contains(p.WorkFlowStage)
                         )
                         .Select(x=>x);
    return View(myPOs);
}

Open in new window


Giannis
Avatar of finance_teacher
finance_teacher

ASKER

How can I change below #1 so URLRole gets passed ?
Currently it gets passed as "NULL" ?
----------------------------------------------
Steps
 1. user clicks
    <li>@Html.ActionLink("TEST", "Index", "MAINT_WORK_REQ", new { area = "TEST" }, new { URLRole = "Inspector" })</li>
----------------------------------------------
 2. parameters get passed to below CONTROLLER method
        public ActionResult Index(string URLRole)
        {
            string sabProgramID = "FORD";
            var myPOs = from p in db.MAINT_WORK_REQ
                        select p;

            switch (URLRole)
            {
                case "Inspector":
                    myPOs = myPOs.Where(pI => pI.WorkFlowStage == 2 && pI.ProgramID == sabProgramID);
                    break;
                case "Lead":
                    List<int?> wfs = new List<int?>() { 6, 7 };
                    myPOs = myPOs.Where(pL => wfs.Contains(pL.WorkFlowStage) && pL.ProgramID == sabProgramID);
                    break;
                case "Customer":
                    myPOs = myPOs.Where(pC => pC.WorkFlowStage == 4 && pC.ProgramID == sabProgramID);
                    break;
            }
            return View(myPOs.ToList());
        }
Below works when I am already inside the AREA
  ** i.e. http://localhost:64587/TEST/Anypage
 @Html.ActionLink("Test2", "Index", "MAINT_WORK_REQ", new { URLRole = "Inspector" }, null)

How can I get below working when outside the AREA
  ** i.e. http://localhost:64587/Anypage
<li>@Html.ActionLink("TEST", "Index", "MAINT_WORK_REQ", new { area = "TEST" }, new { URLRole = "Inspector" })</li>
Hi finance_teacher;

To your question, "How can I change below #1 so URLRole gets passed ?", What is the exception you are getting?
No error, it just passes URLRole as NULL when using the below link outside the AREA
I want it to pass URLRole = Inspector

<li>@Html.ActionLink("TEST", "Index", "MAINT_WORK_REQ", new { area = "TEST" }, new { URLRole = "Inspector" })</li>
I am out of my comfort zone with ASP.Net MVC buy try this,

<li>@Html.ActionLink("TEST", "Index", "MAINT_WORK_REQ", new { area = "TEST" }, "Inspector")</li>

Open in new window

I got the same NULL parameter result when doing the below.
How can I get the URLRole parameter to run outside the AREA ?

<li>@Html.ActionLink("TEST", "Index", "MAINT_WORK_REQ", new { area = "TEST" }, "Inspector")</li>
Try this.

<li>@Html.ActionLink("TEST", "Index", "MAINT_WORK_REQ", "TEST", "Inspector")</li>

Open in new window

That also fails with below error since I have multiple areas with the same MAINT_WORK_REQController file name.

The request for 'MAINT_WORK_REQ' has found the following matching controllers:
Areas.Testing.Controllers.MAINT_WORK_REQController
Areas.__Testing.Controllers.MAINT_WORK_REQController

I really need the new { area = "TEST" } added when doing the link somehow.
Sorry finance_teacher but I am not that up on MVC and can not give you a way to accomplish this.

You may want to post this part of the question in the ASP.Net forum.