Reputation: 35
Below is my code and the output I get from it. I get the correct output but I need to calculate the total price and I am not sure how to go about it.
Any help is much appreciated
// 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");
}
Upvotes: 1
Views: 64
Reputation: 2761
Since test
is a List
that contains all the elements that you want to sum, you can use Linq to calculate the total like this:
var total = test.Sum(x => x.priceP);
In Addition to that I would introduce a temporary variable for those calls:
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;
like this:
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;
This should improve performance because you prevent multiple database calls which could have happened before
Upvotes: 3