gpt4 book ai didi

c# - SQL 和 C# - 我有一个查询,它提供了我想要的所有详细信息,但我不确定如何获得总价

转载 作者:太空宇宙 更新时间:2023-11-03 22:52:30 25 4
gpt4 key购买 nike

下面是我的代码和我从中得到的输出。我得到了正确的输出,但我需要计算总价,但我不确定如何去做。

非常感谢任何帮助

// 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;
int hourS = query1.FirstOrDefault().Hours;
DateTime dateS = query1.FirstOrDefault().Date;

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
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,
priceP = d.Price,
hourS = b.Hours
}).ToList();

Console.WriteLine();
Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
Console.WriteLine("CAR SERVICE DETAILS: \t\t " + carMake + " " + carModel);
Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - " + "\n");

Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
Console.WriteLine("LAST SERVICE TOOK PLACE ON:\t " + dateS.ToShortDateString());
Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - " + "\n");

Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
Console.WriteLine("HOURS SPENT ON THE SERVICE:\t " + hourS);
Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - " + "\n");

Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
Console.WriteLine("PARTS USED: \t\t\tPRICE PART");
Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - " + "\n");

foreach (var item in test)
{
Console.WriteLine("\t\t\t " + item.priceP + "\t " + item.replacedParts);
}

Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - " + "\n");
}

enter image description here

最佳答案

由于 test 是一个 List,其中包含您要求和的所有元素,因此您可以使用 Linq 来计算总数,如下所示:

var total = test.Sum(x => x.priceP);

除此之外,我将为这些调用引入一个临时变量:

int carID = query.FirstOrDefault().Id;
string carRegg = query.FirstOrDefault().regNo;
string carMake = query.FirstOrDefault().Make;
string carModel = query.FirstOrDefault().Model;
int hourS = query1.FirstOrDefault().Hours;
DateTime dateS = query1.FirstOrDefault().Date;

像这样:

var result = query.FirstOrDefault();
int carID = result.Id;
string carRegg = result.regNo;
string carMake = result.Make;
string carModel = result.Model;
int hourS = result.Hours;
DateTime dateS = result.Date;

这应该会提高性能,因为您可以防止之前可能发生的多个数据库调用

关于c# - SQL 和 C# - 我有一个查询,它提供了我想要的所有详细信息,但我不确定如何获得总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46818400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com