0

I have the following lamba query that seems to always return all records even when the int? status pamameter is not null and valid. Anyone know why is the Enum filter not being honored? Thanks for reading!

public ActionResult GetArrivals(int facilityId, int? status)
{
    var registrationList = db.Registrations
            .Where(f => f.Facility.Id == facilityId)
            .Where(a => a.ArrivalDateTime >= DateTime.Today)
            .OrderBy(ln => ln.User.LastName)
            .OrderBy(fn => fn.User.FirstName);


    if (status.HasValue)
    {
        UrgentCareWaitWeb.Models.RegistrationStatus statusValue;

        if (Enum.TryParse(status.Value.ToString(), out statusValue) == true)
        {
            registrationList
                .Where(s => s.Status == statusValue);
        } 
    }



    // Project query to view
    var pview = registrationList.Select(r => new PatientRegistrationsView()
            {
                ArrivalId = r.Id,
                UserId = r.User.UserId,
                UserName = r.User.UserName,
                FirstName = r.User.FirstName,
                LastName = r.User.LastName,
                PrimaryPhone = r.User.PrimaryPhone,
                SecondaryPhone = r.User.SecondaryPhone,
                ArrivalDateTime = r.ArrivalDateTime,
                PatientDataformId = r.PatientDataformId,
                RegistrationStatus = r.Status,

            });



    //if (Request.IsAjaxRequest())
    //  return PartialView("_PatientRegistrations", model);


    return View(pview);
}

3 Answers 3

5

You need to point the result of the Where method back to the original object - it returns a new IEnumerable instead of doing an in-place change.:

registrationList = registrationList.Where(s => s.Status == statusValue);

In response to the update: you'll need to swap out where your order clause goes. Try:

var registrationList = db.Registrations
        .Where(f => f.Facility.Id == facilityId)
        .Where(a => a.ArrivalDateTime >= DateTime.Today);

if (status.HasValue)
{
    UrgentCareWaitWeb.Models.RegistrationStatus statusValue;

    if (Enum.TryParse(status.Value.ToString(), out statusValue) == true)
    {
        registrationList = registrationList
            .Where(s => s.Status == statusValue);
    } 
}

 var pview = registrationList
            .OrderBy(ln => ln.User.LastName)
            .ThenBy(fn => fn.User.FirstName)
            .Select(r => new PatientRegistrationsView()
        {
            ArrivalId = r.Id,
            UserId = r.User.UserId,
            UserName = r.User.UserName,
            FirstName = r.User.FirstName,
            LastName = r.User.LastName,
            PrimaryPhone = r.User.PrimaryPhone,
            SecondaryPhone = r.User.SecondaryPhone,
            ArrivalDateTime = r.ArrivalDateTime,
            PatientDataformId = r.PatientDataformId,
            RegistrationStatus = r.Status,

        });

Note the additional use of ThenBy - this will order by LastName then FirstName; the original would order by LastName, but then replace that ordering with FirstName.

Sign up to request clarification or add additional context in comments.

2 Comments

Much appreciate the response. I replaced that code with the one you posted but it does not compile. Cannot implicitly convert type 'System.Linq.IQueryable<UrgentCareWaitWeb.Models.UrgentCareRegistration>' to 'System.Linq.IOrderedQueryable<UrgentCareWaitWeb.Models.UrgentCareRegistration>'. An explicit conversion exists (are you missing a cast?)
That probably works but I found from another post that replacing the "var" declaration with an explicit IQueryable<UrgentCareRegistration> registrationList AND applying your code it it works fine. Thank you so much!
0

All LINQ queries return an IEnumerable<T> objects, but they do not change the original sequence.

do this:

registrationList = registrationList.Where(s => s.Status == statusValue);

1 Comment

@eouw0o83hf yeah saw the select he is doing .. changed it. Thanks :-)
0

I think this might be also linked to closure of variables within lambda expressions...

use a variable internally like _internalfacilityID and assign the incoming parameter value to it first.

can you please check if this also helps to solve your problem even if you solved it using other ways.

public ActionResult GetArrivals(int facilityId, int? status)
{
    int _internalfacilityID = facilityId;
    var registrationList = db.Registrations
        .Where(f => f.Facility.Id == **_internalfacilityID**)
        .Where(a => a.ArrivalDateTime >= DateTime.Today)
        .OrderBy(ln => ln.User.LastName)
        .OrderBy(fn => fn.User.FirstName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.