ChiliYago
ChiliYago

Reputation: 12289

Lambda Query filter by Enum

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);
}

Upvotes: 0

Views: 1193

Answers (3)

Ashish Gupta
Ashish Gupta

Reputation: 71

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);

Upvotes: 0

eouw0o83hf
eouw0o83hf

Reputation: 9588

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.

Upvotes: 5

Aniket Inge
Aniket Inge

Reputation: 25695

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);

Upvotes: 0

Related Questions