Posting Questions
Posting Questions

Reputation: 217

Why MAX is not working with where in Entity Framework?

I am new to EF and writing this to select then max record that contains the combination. Now i want to pick MAX of those records only.

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

I tried everything to pick the max InspectionReportID record in where but nothing works

Upvotes: 0

Views: 84

Answers (1)

Amir Molaei
Amir Molaei

Reputation: 3810

Order by the specified column (inspectionReportID) descendingly then take the first record:

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

Upvotes: 3

Related Questions