简体   繁体   中英

c# SQL - how get the most recent date of a customers last recent car service

how do i get the most recent date of a customers last recent car service

The below pulls the data but not sure how to get just the most recent dates

//Search Database
            if (query.Any()) 
            {
                int carID = query.FirstOrDefault().Id;
                string carRegg = query.FirstOrDefault().regNo;
                string carMake = query.FirstOrDefault().Make;
                string carModel = query.FirstOrDefault().Model;

                var test = (from a in dbC.Cars
                            where a.Id == carID
                            join b in dbC.Services on a.Id equals b.CarId
                            join c in dbC.PartsUseds on b.ServiceWrkNo equals c.ServiceServiceWrkNo
                            join d in dbC.Parts on c.PartsPartNo equals d.PartNo
                            select new
                            {
                                serviceNum = b.ServiceWrkNo,
                                date = b.Date,
                                PartNo = c.PartsUsedNo,
                                replacedParts = d.PartName
                            }).ToList();
                Console.WriteLine();
                Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
                Console.WriteLine("CAR SERVICE DETAILS: " + carRegg + " " + carMake + " " + carModel);
                Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - " + "\n");
                Console.WriteLine("ServiceNo \t DATE \t Items Replaced \t Cost");
                foreach (var item in test)
                {
                    float cost = item.PartNo + item.PartNo;
                    Console.WriteLine(item.serviceNum + "\t\t   " + item.date.ToShortDateString() + "\t\t  " + cost);
                }
            }
var test = (from a in dbC.Cars
                        where a.Id == carID
                        join b in dbC.Services on a.Id equals b.CarId
                        join c in dbC.PartsUseds on b.ServiceWrkNo equals c.ServiceServiceWrkNo
                        join d in dbC.Parts on c.PartsPartNo equals d.PartNo
                        orderby b.Date descending
                        select new
                        {
                            serviceNum = b.ServiceWrkNo,
                            date = b.Date,
                            PartNo = c.PartsUsedNo,
                            replacedParts = d.PartName
                        }).ToList();

orderby b.Date descending to order the records by the date value with the most recent being the first.

.Take(5).ToList(); to only show the 5 most recent, as an example.

Or you could try something like this - only returns data with the max service date

var test = (from a in dbC.Cars                            
            join b in dbC.Services on a.Id equals b.CarId
            join c in dbC.PartsUseds on b.ServiceWrkNo equals c.ServiceServiceWrkNo
            join d in dbC.Parts on c.PartsPartNo equals d.PartNo
            where a.Id == carID && b.Date == ((from b1 in dbC.Services where b1.CarId == b.CarId select b1.Date).Max())
            select new
            {
                serviceNum = b.ServiceWrkNo,
                date = b.Date,
                PartNo = c.PartsUsedNo,
                replacedParts = d.PartName
            }).FirstOrDefault();

If we have navigational properties (already automatically created from the foreign keys), I would mention that you can do things like the psuedo-code below:

    var lastService = (from s in dbC.Services
                       where s.CarId = carID
                       orderby s.Date descending
                       select new
                       {
                           s.ServiceWrkNo,
                           s.Date,
                           ServiceParts = s.PartsUsed
                       }).FirstOrDefault();

This is to show the concepts of joins versus navigational properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM