从电网LINQ查询一个问题,显示数据 [英] A problems with display data from linq queries in grid

查看:88
本文介绍了从电网LINQ查询一个问题,显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做这个查询。 LinqSQl

  VAR的查询=从订单中mydb.Orders
                    加盟客户在mydb.Customers上order.CustomerID等于customer.CustomerID
                    加入的员工在mydb.Employees上order.EmployeeID等于employee.EmployeeID
                    加入托运人mydb.Shippers上order.ShipVia等于shipper.ShipperID
                    在mydb.Order_Details加入的OrderDetail上order.OrderID等于orderdetail.OrderID
                    在mydb.Products加入产品。orderdetail.ProductID等于product.ProductID
                    新选择
                                 {
                                     订单id = order.OrderID,
                                     客户名称= customer.CompanyName,
                                     EmployeeName = employee.FirstName,
                                     ShipperName = shipper.CompanyName,
                                     产品= product.ProductName,
                                     TotalPrice = orderdetail.UnitPrice * orderdetail.Quantity                                 }
                        为D-
                        通过d.OrderID D组;

然后我要显示数据网格这一数据

还有就是我的标记:

 <%@页标题=主页LANGUAGE =C#AutoEventWireup =真codeBehind =Default.aspx.cs继承=ASP1。 _Default%GT;
 <!DOCTYPE HTML>
  < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
 <头ID =头像1=服务器>
 < /头>
  <身体GT;
<表ID =form1的=服务器>
< D​​IV>
     < ASP:GridView控件ID =网格=服务器的AutoGenerateColumns =假
           CELLPADDING =4前景色=#333333宽度=600px的>
        <柱体和GT;
             < ASP:BoundField的数据字段=订单ID的HeaderText =名称/>
             < ASP:BoundField的数据字段=客户名称的HeaderText =客户名称/>
             < ASP:BoundField的数据字段=EmployeeName的HeaderText =EmployeeName/>
             < ASP:BoundField的数据字段=ShipperName的HeaderText =ShipperName/>
             < ASP:BoundField的数据字段=产品的HeaderText =产品/>
             < ASP:BoundField的数据字段=TotalPrice的HeaderText =TotalPrice/>
        < /专栏>
        < HeaderStyle背景色=#7961daFONT-粗体=真前景色=白/>
        < RowStyle的BackColor =#F7F6F3前景色=#333333/>
      < / ASP:GridView的>
< / DIV>
< /表及GT;
< /身体GT;
 < / HTML>

这样的方法

  Grid.DataSource = query.ToList();
         Grid.DataBind();

和我得到的错误:一个字段或属性名称为订单ID没有选择的数据源上找到。

什么是我的问题吗?

,因为当我使用此查询

  VAR QUERY1 =从订单中mydb.Orders
                     加盟客户在mydb.Customers上order.CustomerID等于customer.CustomerID
                     加入的员工在mydb.Employees上order.EmployeeID等于employee.EmployeeID
                     加入托运人mydb.Shippers上order.ShipVia等于shipper.ShipperID
                     在mydb.Order_Details加入的OrderDetail上order.OrderID等于orderdetail.OrderID
                     在mydb.Products加入产品。orderdetail.ProductID等于product.ProductID                     新选择
                     {                         订单id = order.OrderID,
                         客户名称= customer.CompanyName,
                         EmployeeName = employee.FirstName,
                         ShipperName = shipper.CompanyName,
                         产品= product.ProductName,
                         TotalPrice = orderdetail.UnitPrice * orderdetail.Quantity
                     };
                                 Grid.DataSource = QUERY1;
                                 Grid.DataBind();

我显示数据,但它们不进行分组。

我得到这个数据


的OrderId |客户名称| EmployeeName | ShipperName |产品| TotalPrice1苹果凯文AIG iPhone 10001凯文苹果IPAD AIG 11002三星约翰KMP S6 9002三星约翰KMP S5 800

我要得到这样的结果。


的OrderId |客户名称| EmployeeName | ShipperName |产品| TotalPrice1苹果凯文AIG的iPhone,iPad 21002三星约翰KMP S6,S5 1700


解决方案

当您在LINQ前pression使用它改变了结果的类型从的IEnumerable&LT; AnonType&GT; 的IEnumerable&LT; IGrouping&LT; INT,AnonType&GT;&GT; 的OrderID apears是一个 INT (请参阅方法组的文档通过使用在< A HREF =htt​​ps://msdn.microsoft.com/en-us/library/bb534501%28v=vs.100%29.aspx相对=nofollow> MSDN )。

因此​​,你不能只显示为匿名类型的简单列表。

如果您想订购由的OrderId 的结果的话,组是在浪费时间:使用按排序。

如果您要处理的每个对象的所有订单ID 然后你需要组。

修改 根据修正的问题:

显示数据之前,它似乎要通过的OrderId 来总结运行,specically:


  • 总结 TotalPrice

  • 加入 Prodcts 用逗号分隔符。

这是很容易做到,但将意味着创建新实例。从previous查询与 groupedData 启动:

  VAR outputData = groupedData.Select(G =&gt;新建{
                                      的OrderId = g.Key,
                                      [...]
                                      产品=的string.join(,,g.Select(X =&GT; x.Products)),
                                      TotalPrice = g.Select(X =&GT; x.TotalPrice).SUM()
                                    });

组中的每个对象是一个 IGrouping&LT; INT,AnonType&GT; 这是一个的IEnumerable&LT; AnonType&GT; 一个额外的属性(它是由分组);从而可以使用通常的LINQ运营商来进行聚合。

I make this query. LinqSQl

         var query = from order in mydb.Orders
                    join customer in mydb.Customers on order.CustomerID    equals customer.CustomerID
                    join employee in mydb.Employees on order.EmployeeID equals employee.EmployeeID
                    join shipper in mydb.Shippers on order.ShipVia equals shipper.ShipperID
                    join orderdetail in mydb.Order_Details on order.OrderID equals orderdetail.OrderID
                    join product in mydb.Products on orderdetail.ProductID equals product.ProductID
                    select new
                                 {
                                     OrderID = order.OrderID,
                                     CustomerName = customer.CompanyName,
                                     EmployeeName = employee.FirstName,
                                     ShipperName = shipper.CompanyName,
                                     Products = product.ProductName,
                                     TotalPrice = orderdetail.UnitPrice * orderdetail.Quantity

                                 }
                        into d
                        group d by d.OrderID;

then I want display this data in datagrid

there is my markup:

              <%@ Page Title="Home Page" Language="C#"  AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="asp1._Default" %>
 <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
 </head>
  <body>
<form id="form1" runat="server">
<div>
     <asp:GridView ID="Grid" runat="server" AutoGenerateColumns="false"  
           CellPadding="4" ForeColor="#333333"  Width="600px">
        <Columns>
             <asp:BoundField DataField="OrderID" HeaderText="Name" />
             <asp:BoundField DataField="CustomerName" HeaderText="CustomerName" />
             <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" />
             <asp:BoundField DataField="ShipperName" HeaderText="ShipperName" />
             <asp:BoundField DataField="Products" HeaderText="Products" />
             <asp:BoundField DataField="TotalPrice" HeaderText="TotalPrice" />
        </Columns>   
        <HeaderStyle BackColor="#7961da"  Font-Bold="True" ForeColor="White" />         
        <RowStyle BackColor="#F7F6F3"  ForeColor="#333333" />
      </asp:GridView>
</div>
</form>
</body>
 </html>

thus method

         Grid.DataSource = query.ToList();
         Grid.DataBind();

And I get error: A field or property with the name 'OrderID' was not found on the selected data source.

What is my problem?

because when I use this query

  var query1 = from order in mydb.Orders
                     join customer in mydb.Customers on order.CustomerID    equals customer.CustomerID
                     join employee in mydb.Employees on order.EmployeeID equals employee.EmployeeID
                     join shipper in mydb.Shippers on order.ShipVia equals shipper.ShipperID
                     join orderdetail in mydb.Order_Details on order.OrderID equals orderdetail.OrderID
                     join product in mydb.Products on orderdetail.ProductID equals product.ProductID

                     select new
                     {

                         OrderID = order.OrderID,
                         CustomerName = customer.CompanyName,
                         EmployeeName = employee.FirstName,
                         ShipperName = shipper.CompanyName,
                         Products = product.ProductName,
                         TotalPrice = orderdetail.UnitPrice * orderdetail.Quantity
                     };
                                 Grid.DataSource = query1;
                                 Grid.DataBind();

I display data, but they aren't grouped.

I get this data

OrderId| CustomerName| EmployeeName |ShipperName | Products| TotalPrice

1          Apple        Kevin            AIG        Iphone     1000

1          Apple        Kevin            AIG        IPAD     1100

2         Samsung          John           KMP        S6       900

2         Samsung          John           KMP        S5       800

I want to get this result.

OrderId| CustomerName| EmployeeName |ShipperName | Products   | TotalPrice

1          Apple        Kevin            AIG       Iphone,Ipad     2100

2         Samsung          John           KMP        S6,s5       1700

解决方案

When you use group by in the LINQ expression it changes the type of the result from IEnumerable<AnonType> to IEnumerable<IGrouping<int, AnonType>> as OrderID apears to be a int (see the documentation for the method group by uses on MSDN).

Thus you cannot just display as a simple list of the anonymous type.

If you want to order the results by OrderId then group by is a waste of time: use order by to sort.

If you want to process all objects for each OrderID then you need to group.

Edit based on amended question:

Before displaying the data, it appears you want to sum up the runs by OrderId, specically:

  • Sum up TotalPrice
  • Join the Prodcts with comma separators.

This is easy enough to do, but will mean creating new instances. Starting with groupedData from previous query:

var outputData = groupedData.Select(g => new {
                                      OrderId = g.Key,
                                      […]
                                      Products = String.Join(", ", g.Select(x => x.Products)),
                                      TotalPrice = g.Select(x => x.TotalPrice).Sum()
                                    });

Each object from the group by is a IGrouping<int, AnonType> which is an IEnumerable<AnonType> with an additional Key property (what it is grouped by); thus can use the usual LINQ operators to perform the aggregations.

这篇关于从电网LINQ查询一个问题,显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆