【问题标题】:Lambda Query filter by Enum按枚举的 Lambda 查询过滤器
【发布时间】:2013-03-08 22:13:38
【问题描述】:

我有以下 Lamba 查询,它似乎总是返回所有记录,即使是 int?状态参数不为空且有效。任何人都知道为什么 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);
}

【问题讨论】:

    标签: asp.net-mvc linq lambda


    【解决方案1】:

    您需要将 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

    【讨论】:

    • 非常感谢您的回复。我用您发布的代码替换了该代码,但它无法编译。无法将类型“System.Linq.IQueryable”隐式转换为“System.Linq.IOrderedQueryable”。存在显式转换(您是否缺少演员表?)
    • 这可能有效,但我从另一篇文章中发现,用显式 IQueryable registrationList 替换“var”声明并应用您的代码它工作正常。非常感谢!
    【解决方案2】:

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

    这样做:

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

    【讨论】:

    • @eouw0o83hf 是的,看到他正在做的select .. 改变了它。谢谢:-)
    【解决方案3】:

    我认为这也可能与 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多