为什么MAX不在Entity Framework中的哪个位置?

问题描述 投票:0回答:1

我是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;
        }

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

c# asp.net-mvc entity-framework linq where-clause
1个回答
3
投票

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

VelosiReportNo = (string)query
                          .Where(x => x.VelosiReportNo.Contains(OfficeStationCombination))
                          .OrderByDesc(x => x.inspectionReportID)
                          .FirstOrDefault().VelosiReportNo.ToString(); 
© www.soinside.com 2019 - 2024. All rights reserved.