LINQ 实体框架 - 计数和查询

发布于 2024-12-13 18:44:38 字数 2142 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-12-20 18:44:38

根据评论编辑了答案。
我将首先定义一个类来合并查询结果:

public class OrderDataResults // Or whatever you want to call it
{
    public int DriverId { get; set; }
    public int OrderCount { get; set; }
    public int OrderCountWhereNameIsNotNull { get; set; }
}

您可以从服务器返回这些结果的列表:

public class OrderViewRepository : BaseRepository<OrderView>
{
    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();
    }
}

加载页面时,您可以调用 GetOrderDataResults() 方法并获取列表:

protected void Page_Load(object sender, EventArgs e)
{
    var rep = new OrderViewRepository();
    List<OrderDataResults> results = rep.GetOrderDataResults();

    DataViewer.DataSource = results;
    DataViewer.DataBind();
}

为了查看结果,只需从 GridView 定义中删除 部分:

<asp:GridView runat="server" ID="DataViewer">
</asp:GridView>

同样,您可能需要重构此代码并做出一些结构决策一旦你得到这个,就由你自己代码工作(例如,决定是否要从存储库返回列表等)。
此外,您可能需要自定义表格的外观,因此您可能需要查看 GridView 文档。在您的情况下,您似乎只想显示数据,因此需要更简单的控件(例如 Repeater) 可能更适合。

Edited the answer according to the comments.
I would define a class to incorporate the results of your query first:

public class OrderDataResults // Or whatever you want to call it
{
    public int DriverId { get; set; }
    public int OrderCount { get; set; }
    public int OrderCountWhereNameIsNotNull { get; set; }
}

From your server you can return a list of those:

public class OrderViewRepository : BaseRepository<OrderView>
{
    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();
    }
}

And when loading the page you can call the GetOrderDataResults() method and get your list:

protected void Page_Load(object sender, EventArgs e)
{
    var rep = new OrderViewRepository();
    List<OrderDataResults> results = rep.GetOrderDataResults();

    DataViewer.DataSource = results;
    DataViewer.DataBind();
}

In order to see the results, just remove the <Columns> section from your GridView definition:

<asp:GridView runat="server" ID="DataViewer">
</asp:GridView>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文