C# LINQ查询以获取数据
我有4个XML文件,其中包含这种格式的数据。这些数据实际上来自Microsoft Northwind数据库,但我有一些XML格式的表。完整的关系图可用 订单C# LINQ查询以获取数据,c#,linq,join,group-by,multiple-tables,C#,Linq,Join,Group By,Multiple Tables,我有4个XML文件,其中包含这种格式的数据。这些数据实际上来自Microsoft Northwind数据库,但我有一些XML格式的表。完整的关系图可用 订单 <Orders> <Order> <OrderID>10248</OrderID> <CustomerID>VINET</CustomerID> <EmployeeID>5</EmployeeID> </Or
<Orders>
<Order>
<OrderID>10248</OrderID>
<CustomerID>VINET</CustomerID>
<EmployeeID>5</EmployeeID>
</Order>
.............
.............
.............
<OrderDetails>
<OrderDetail>
<OrderID>10248</OrderID>
<Quantity>12</Quantity>
<UnitPrice>14.0000</UnitPrice>
</OrderDetail>
<OrderDetail>
<OrderID>10248</OrderID>
<Quantity>10</Quantity>
<UnitPrice>9.8000</UnitPrice>
</OrderDetail>
.............
.............
.............
<Employee>
<EmployeeID>5</EmployeeID>
<FirstName>Steve</FirstName>
<LastName>Buchanan</LastName>
</Employee>
<Employee>
<EmployeeID>6</EmployeeID>
<FirstName>Michael</FirstName>
<LastName>Suyama</LastName>
</Employee>
.............
.............
.............
<Customer>
<CustomerID>VINET</CustomerID>
<CompanyName>Vins et alcools Chevalier</CompanyName>
<ContactName>Paul Henriot</ContactName>
</Customer>
<Customer>
<CustomerID>WANDK</CustomerID>
<CompanyName>Die Wandernde Kuh</CompanyName>
<ContactName>Rita Müller</ContactName>
</Customer>
<Customer>
.............
.............
.............
但这只给了我三件事。我正在努力获取对象包含所需的全部6种内容。另外,由于orders表中的一个orderid可以有多个orderdetails,因此如何执行计算。例如,对于订单id 10248,我们有2个订单详细信息
谢谢您需要多个连接和分组,如下所示
var list = from o in ordersList
join cl in customersList
on o.CustomerID equals cl.CustomerID
join ol in orderDetailsList
on o.OrderID equals ol.OrderID
join e in employeeList
on o.EmployeeID equals e.EmployeeID
select new
{
o.OrderID,
cl.CompanyName,
cl.ContactName,
EmployeeName = e.FirstName + " " +e.LastName,
ol.Quantity,
ol.UnitPrice
};
var result = list.GroupBy(x => x.OrderID).Select(g => new
{
OrderID = g.Key,
CompanyName = g.First().CompanyName,
ContactName = g.First().ContactName,
EmployeeName = g.First().EmployeeName,
TotalQuantity = g.Sum(x => x.Quantity),
TatalPrice = g.Sum(x => x.Quantity * x.UnitPrice)
});
您需要多个联接和分组,如下所示
var list = from o in ordersList
join cl in customersList
on o.CustomerID equals cl.CustomerID
join ol in orderDetailsList
on o.OrderID equals ol.OrderID
join e in employeeList
on o.EmployeeID equals e.EmployeeID
select new
{
o.OrderID,
cl.CompanyName,
cl.ContactName,
EmployeeName = e.FirstName + " " +e.LastName,
ol.Quantity,
ol.UnitPrice
};
var result = list.GroupBy(x => x.OrderID).Select(g => new
{
OrderID = g.Key,
CompanyName = g.First().CompanyName,
ContactName = g.First().ContactName,
EmployeeName = g.First().EmployeeName,
TotalQuantity = g.Sum(x => x.Quantity),
TatalPrice = g.Sum(x => x.Quantity * x.UnitPrice)
});
您需要多个联接和分组,如下所示
var list = from o in ordersList
join cl in customersList
on o.CustomerID equals cl.CustomerID
join ol in orderDetailsList
on o.OrderID equals ol.OrderID
join e in employeeList
on o.EmployeeID equals e.EmployeeID
select new
{
o.OrderID,
cl.CompanyName,
cl.ContactName,
EmployeeName = e.FirstName + " " +e.LastName,
ol.Quantity,
ol.UnitPrice
};
var result = list.GroupBy(x => x.OrderID).Select(g => new
{
OrderID = g.Key,
CompanyName = g.First().CompanyName,
ContactName = g.First().ContactName,
EmployeeName = g.First().EmployeeName,
TotalQuantity = g.Sum(x => x.Quantity),
TatalPrice = g.Sum(x => x.Quantity * x.UnitPrice)
});
您需要多个联接和分组,如下所示
var list = from o in ordersList
join cl in customersList
on o.CustomerID equals cl.CustomerID
join ol in orderDetailsList
on o.OrderID equals ol.OrderID
join e in employeeList
on o.EmployeeID equals e.EmployeeID
select new
{
o.OrderID,
cl.CompanyName,
cl.ContactName,
EmployeeName = e.FirstName + " " +e.LastName,
ol.Quantity,
ol.UnitPrice
};
var result = list.GroupBy(x => x.OrderID).Select(g => new
{
OrderID = g.Key,
CompanyName = g.First().CompanyName,
ContactName = g.First().ContactName,
EmployeeName = g.First().EmployeeName,
TotalQuantity = g.Sum(x => x.Quantity),
TatalPrice = g.Sum(x => x.Quantity * x.UnitPrice)
});
谢谢你,伙计。我有很多东西要学。谢谢你的时间,谢谢你,伙计。我有很多东西要学。谢谢你的时间,谢谢你,伙计。我有很多东西要学。谢谢你的时间,谢谢你,伙计。我有很多东西要学。谢谢你抽出时间。