我做了这个查询。 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;
然后我想在datagrid中显示这些数据
有我的标记:
<%@ 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>
因此方法
Grid.DataSource = query.ToList();
Grid.DataBind();
我收到错误:在所选数据源上找不到名为“OrderID”的字段或属性。
我的问题是什么?
因为当我使用此查询时
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();
我显示数据,但它们没有分组。
我得到这个数据
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
我想得到这个结果。
OrderId| CustomerName| EmployeeName |ShipperName | Products | TotalPrice 1 Apple Kevin AIG Iphone,Ipad 2100 2 Samsung John KMP S6,s5 1700
答案 0 :(得分:1)
当您在LINQ表达式中使用group by
时,它将结果的类型从IEnumerable<AnonType>
更改为IEnumerable<IGrouping<int, AnonType>>
,因为OrderID
apears为int
(请参阅group by
上OrderId
使用的方法的文档。
因此,您不能只显示匿名类型的简单列表。
如果您想按group by
订购结果,那么order by
是浪费时间:使用OrderID
进行排序。
如果要处理每个OrderId
的所有对象,则需要进行分组。
编辑 基于修正后的问题:
在显示数据之前,您似乎想要按TotalPrice
总结运行,特别是:
Prodcts
groupedData
。这很容易做到,但意味着要创建新实例。从上一个查询的var outputData = groupedData.Select(g => new {
OrderId = g.Key,
[…]
Products = String.Join(", ", g.Select(x => x.Products)),
TotalPrice = g.Select(x => x.TotalPrice).Sum()
});
开始:
group by
IGrouping<int, AnonType>
中的每个对象都是IEnumerable<AnonType>
,Key
具有额外的{{1}}属性(按其分组);因此可以使用通常的LINQ运算符来执行聚合。