简体   繁体   English

LINQ查询以获取数据

[英]LINQ Query to get data

I have got 4 XML files that contain data in this format. 我有4个XML文件,其中包含这种格式的数据。 This data is actually from Microsoft Northwind database but I have got some tables in the XML format. 这些数据实际上来自Microsoft Northwind数据库,但是我有一些XML格式的表。 The full relationship diagram is available here . 完整的关系图可在此处获得

Orders 命令

<Orders>
  <Order>
    <OrderID>10248</OrderID>
    <CustomerID>VINET</CustomerID>
    <EmployeeID>5</EmployeeID>
  </Order>
.............
.............
.............

OrderDetails 订单详细信息

<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>
.............
.............
.............

Employees 雇员

<Employee>
    <EmployeeID>5</EmployeeID>
    <FirstName>Steve</FirstName>
    <LastName>Buchanan</LastName>
  </Employee>
  <Employee>
    <EmployeeID>6</EmployeeID>
    <FirstName>Michael</FirstName>
    <LastName>Suyama</LastName>
  </Employee>
.............
.............
.............

Customers 顾客

<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>
.............
.............
.............

Now I want to get a list of objects such that each object contains the following: 现在,我想获取一个对象列表,以便每个对象包含以下内容:

  1. OrderId (from the Orders table - example - 10248) OrderId(来自“订单”表-示例-10248)
  2. Company Name for the above order id (from Customers table - example - Vins et alcools Chevalier) 上述订单编号的公司名称(来自“客户”表-示例-Vins et alcools Chevalier)
  3. Contact Name for the above order id (from Customers table - example - Paul Henriot) 上述订单ID的联系人姓名(来自“客户”表-示例-Paul Henriot)
  4. Employee Name for an employee id for the corressponding order id (from Employees table - example - Steve Buchanan) 相应订单ID的员工ID的员工名称(来自“员工”表-示例-史蒂夫·布坎南)
  5. Total Quantity for the above order id from OrderDetails table. OrderDetails表中上述订单ID的总数量。 This will be 12 + 10 = 22 because there are two orders for order id 10248 这将是12 + 10 = 22,因为订单ID为10248的订单有两个
  6. Total Price for the above order id from the OrderDetails table. OrderDetails表中上述订单ID的总价。 This will be 12*14 + 10*9.8 = 266. 这将是12 * 14 + 10 * 9.8 = 266。

So one of the objects will look like this - {10248, Vins et alcools Chevalier, Paul Henriot, Steve Buchanan, 22, 266} 所以其中一个对象看起来像这样-{10248,Vins等人,Chevalier,Paul Henriot,Steve Buchanan,22,266}

Now I am able to write a LINQ query to get me orderid, contactname and company name like this: 现在,我可以编写LINQ查询来获取订单号,联系人姓名和公司名称,如下所示:

var list = from o in ordersList
                join cl in customersList
                on o.CustomerId equals cl.CustomerId
                select new
                {
                    o.OrderId,
                    cl.CompanyName,
                    cl.ContactName
                };

But this only gives me three things. 但这只给了我三件事。 I am struggling to get all 6 things that are needed for the object to contain. 我正在努力获取包含对象所需的所有6件事。 Also how to perform the calculation since one orderid in orders table can have multiple orderdetails. 由于订单表中的一个orderid可能具有多个orderdetails,因此该如何执行计算。 For example - for order id 10248 we have 2 order details. 例如-对于订单ID 10248,我们有2个订单详细信息。

Thanks 谢谢

you need multiple joins and group by like below 您需要多个联接并按如下所示进行分组

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)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM