由Enum进行Lambda查询过滤

时间:2013-03-08 22:13:39

标签: asp.net-mvc linq lambda

我有以下lamba查询,即使在int时似乎总是返回所有记录? status pamameter不为null且有效。任何人都知道为什么Enum过滤器不被尊重? 谢谢你的阅读!

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 个答案:

答案 0 :(得分:5)

您需要将Where方法的结果指回原始对象 - 它会返回一个新的IEnumerable而不是进行就地更改。:

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

响应更新:您需要换掉订单子句所在的位置。尝试:

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,

        });

请注意ThenBy的额外使用 - 这将按LastName 然后 FirstName排序;原始文件将按LastName排序,但随后会使用FirstName替换该订单。

答案 1 :(得分:0)

所有LINQ查询都返回IEnumerable<T>个对象,但它们不会更改原始序列。

这样做:

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

答案 2 :(得分:0)

我认为这也可能与lambda表达式中的变量闭包有关......

在内部使用变量,例如 _internalfacilityID ,并首先为其指定传入参数值。

如果您使用其他方式解决问题,请检查这是否也有助于解决您的问题。

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