LINQ 实体框架 - 计数和查询
edmx file: OrderData:
ORDERID
SHIPMENT
DRIVER_ID
RECEIVE_NAME
我们需要为每个 DRIVER_ID 建立一个表并打印 第一列:DRIVER_ID 第二列:有多少行具有此 DRIVER_ID 第三列:有多少行具有此 DRIVER_ID 且 RECEIVE_NAME 不为空,
请告诉我如何完成...我只设法打印 getquery:(
SERVER.CS
public class OrderDataRepository : BaseRepository<OrderData>
{
public class OrderDataResults
{
public long DriverId { get; set; }
public int OrderCount { get; set; }
public int OrderCountWhereNameIsNotNull { get; set; }
}
public class OrderViewRepository : BaseRepository<OrderData>
{
public List<OrderDataResults> GetOrderDataResults()
{
return GetQuery().
Where(x => x.SHIPMENT != null).
GroupBy(o => o.DRIVER_ID).
Select(g =>
new OrderDataResults
{
DriverId = g.Key,
OrderCount = g.Count(),
OrderCountWhereNameIsNotNull =
g.Count(o => o.RECEIVE_NAME != null)
}).ToList();
}
}
CLIENT.ASPX.CS
public partial class Control : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var rep = new OrderViewRepository();
List<OrderDataResults> results = GetOrderDataResults();
**Error 12 The name 'GetOrderDataResults' does not exist in the current context
Error 11 The type or namespace name 'OrderDataResults' could not be found (are you missing a using directive or an assembly reference?)
**
DataViewer.DataSource = results;
DataViewer.DataBind();
}
}
WEBPAGE.ASPX
<form id="Form1" runat="server">
<asp:GridView runat="server" ID="DataViewer">
</asp:GridView>
</form>
edmx file: OrderData:
ORDERID
SHIPMENT
DRIVER_ID
RECEIVE_NAME
we need to build a table and print for each DRIVER_ID
first column:DRIVER_ID
second column: how many rows with this DRIVER_ID
third column: how many rows with this DRIVER_ID and has RECEIVE_NAME not null
please show me how it can be done...i only managed to print a getquery:(
SERVER.CS
public class OrderDataRepository : BaseRepository<OrderData>
{
public class OrderDataResults
{
public long DriverId { get; set; }
public int OrderCount { get; set; }
public int OrderCountWhereNameIsNotNull { get; set; }
}
public class OrderViewRepository : BaseRepository<OrderData>
{
public List<OrderDataResults> GetOrderDataResults()
{
return GetQuery().
Where(x => x.SHIPMENT != null).
GroupBy(o => o.DRIVER_ID).
Select(g =>
new OrderDataResults
{
DriverId = g.Key,
OrderCount = g.Count(),
OrderCountWhereNameIsNotNull =
g.Count(o => o.RECEIVE_NAME != null)
}).ToList();
}
}
CLIENT.ASPX.CS
public partial class Control : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var rep = new OrderViewRepository();
List<OrderDataResults> results = GetOrderDataResults();
**Error 12 The name 'GetOrderDataResults' does not exist in the current context
Error 11 The type or namespace name 'OrderDataResults' could not be found (are you missing a using directive or an assembly reference?)
**
DataViewer.DataSource = results;
DataViewer.DataBind();
}
}
WEBPAGE.ASPX
<form id="Form1" runat="server">
<asp:GridView runat="server" ID="DataViewer">
</asp:GridView>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论编辑了答案。
我将首先定义一个类来合并查询结果:
您可以从服务器返回这些结果的列表:
加载页面时,您可以调用 GetOrderDataResults() 方法并获取列表:
为了查看结果,只需从
GridView
定义中删除
部分:同样,您可能需要重构此代码并做出一些结构决策一旦你得到这个,就由你自己代码工作(例如,决定是否要从存储库返回列表等)。
此外,您可能需要自定义表格的外观,因此您可能需要查看 GridView 文档。在您的情况下,您似乎只想显示数据,因此需要更简单的控件(例如 Repeater) 可能更适合。
Edited the answer according to the comments.
I would define a class to incorporate the results of your query first:
From your server you can return a list of those:
And when loading the page you can call the
GetOrderDataResults()
method and get your list:In order to see the results, just remove the
<Columns>
section from yourGridView
definition:And again, you will probably want to refactor this code and make some structural decisions on your own as soon as you get this code working (e.g. decide whether you want to return a list from your reporitory, etc).
Furthermore, you will probably want to customize the way your table looks, so you might want to look at the GridView documentation. In your case it seems that you only want to display the data, so a simpler control (such as the Repeater) might prove to be a better fit.