首页 > 解决方案 > 为什么 MAX 不能在 Entity Framework 中使用?

问题描述

我是 EF 的新手,写这个是为了选择包含该组合的最大记录。现在我只想挑选MAX那些记录。

        public string GetMaxReportNo(string OfficeStationCombination = "")
        {
            InspectionReport InspectionReport= new InspectionReport();
            string VelosiReportNo="";

            var query = uow.InspectionReportRepository.GetQueryable().AsQueryable();

            if (!string.IsNullOrEmpty(OfficeStationCombination))
            {
                VelosiReportNo = (string)query
                          .Where(x => x.VelosiReportNo.Contains(OfficeStationCombination))
                          .FirstOrDefault().VelosiReportNo.ToString();         
            }

            return VelosiReportNo;
        }

我尝试了一切来选择最大的InspectionReportID记录,where但没有任何效果

标签: c#asp.net-mvcentity-frameworklinqwhere-clause

解决方案


按指定列 ( inspectionReportID)降序排列,然后取第一记录:

VelosiReportNo = (string)query
                          .Where(x => x.VelosiReportNo.Contains(OfficeStationCombination))
                          .OrderByDesc(x => x.inspectionReportID)
                          .FirstOrDefault().VelosiReportNo.ToString(); 

推荐阅读