我有一个 xml 文件,其数据看起来像这样
<Companies>
<Company>
<Name>Company123</Name>
<Sales>
<Sale>
<Code>Sale123</Code>
<Category>Apple</Category>
<Amount>150.50</Amount>
</Sale>
<Sale>
<Code>Sale234</Code>
<Category>Mango</Category>
<Amount>170.50</Amount>
</Sale>
<Sale>
<Code>Sale345</Code>
<Category>Apple</Category>
<Amount>160.50</Amount>
</Sale>
</Sales>
</Company>
<Company>
//other values
</Company>
</Companies>
这是我必须存储此信息的课程
public class Companies
{
public Companies()
{
AllCompanies = new List<Company>();
}
public List<Company> AllCompanies;
}
public class Company
{
public Company()
{
Saleses = new List<Sales>();
}
public string Name;
public string Code;
public double MaintenancePercentage;
public double AverageSales;
public double TotalSales;
public double TotalMaintenanceFee;
public List<Sales> Saleses;
}
public class Sales
{
public string Code;
public string Title;
public DateTime DateTime;
public string Category;
public double Amount;
}
现在我已经能够找到一家公司的总销售额,但我不知道如何找到相同类别的总销售额。这是我所做的示例代码
var companies = (from c in doc.Descendants("Company")
select c).ToList();
foreach (var xCompany in companies)
{
var sales = (from xElement in xCompany.Descendants("Sale") select xElement).ToList();
var company = new Company()
{
Name = ((from x in xCompany.Descendants("Name") select x.Value).First()),
Code = (from x in xCompany.Descendants("Code") select x.Value).First(),
MaintenancePercentage =
Convert.ToDouble((from x in xCompany.Descendants("MaintenancePercentage") select x.Value).First())
};
for (int index = 0; index < sales.Count; index++)
{
var xElement = sales[index];
var sale = new Sales()
{
Code = ((from x in xElement.Descendants("Code") select x.Value).First()),
Title = ((from x in xElement.Descendants("Title") select x.Value).First()),
DateTime = Convert.ToDateTime((((from x in xElement.Descendants("Date") select x.Value).First()))),
Category = ((from x in xElement.Descendants("Category") select x.Value).First()),
Amount = Convert.ToDouble(((from x in xElement.Descendants("Amount") select x.Value).First()))
};
company.Saleses.Add(sale);
}
company.TotalSales = repository.GetTotalSales(company);
company.AverageSales = repository.AverageSales(company);
company.TotalMaintenanceFee = repository.MaintenanceFee(company);
companiesList.AllCompanies.Add(company);
}
#endregion
#region WriteData
foreach (var xElement in companiesList.AllCompanies)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Company Name: " + xElement.Name);
Console.ResetColor();
Console.WriteLine("Company Code: "+xElement.Code);
Console.WriteLine("Company Maintenance percentage: "+xElement.MaintenancePercentage);
Console.WriteLine("Company Total sales: " + xElement.TotalSales);
Console.WriteLine("Total maintenance fee: "+ xElement.TotalMaintenanceFee);
Console.WriteLine("Company Average sales: " + xElement.AverageSales);
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Linked sales:");
Console.ResetColor();
foreach (var sale in xElement.Saleses)
{
Console.WriteLine("Code: "+ sale.Code);
Console.WriteLine("Title: " + sale.Title);
Console.WriteLine("Date: " + sale.DateTime);
Console.WriteLine("Category: " + sale.Category);
Console.WriteLine("Amount: " + sale.Amount);
}
Console.WriteLine(" ");
}
#endregion
Console.ReadLine();
}
}
如何显示匹配类别的总数?