如何获取客户上次最近的汽车服务的最新日期
下面提取数据但不确定如何获取最近的日期
//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);
}
}
答案 0 :(得分:1)
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
按日期值排序记录,最新的记录是第一个。
.Take(5).ToList();
仅显示最近的5个,作为示例。
答案 1 :(得分:0)
或者您可以尝试这样的事情 - 仅返回具有最长服务日期的数据
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();
答案 2 :(得分:0)
如果我们有导航属性(已经从外键自动创建),我会提到你可以做下面的伪代码:
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();
这是为了显示连接与导航属性的概念。