2

我在数据网格中显示处理时间数据,如下所示

在此处输入图像描述

使用以下语句

  var duration_query = this.db.Events
                       .Where(p => ID.Contains(p.ServerID) &&
                       p.Type == "Complete" && 
                      // fromDP and toDP are name of DataPicker control
                       (p.Date >= fromDP.SelectedDate.Value && 
                        p.Date <= toDP.SelectedDate.Value))
                       .GroupBy(p => p.Duration)
                       .Select(g => new
                       {
                       Duration = g.Key,
                       serverID = g.Count()
                       })
                       .OrderBy(x => x.Duration).ToList();

  dgProcessingTime.ItemsSource = duration_query.ToList();

XAML

我已经使用布局变换来旋转数据网格标题。

    <DataGrid x:Name="dgProcessingTime" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="false">     
   <DataGrid.Columns>
     <DataGridTextColumn x:Name="timeColumn" Header="Time in seconds" Binding="{Binding Path=Duration, Mode= OneWay}"></DataGridTextColumn>
     <DataGridTextColumn x:Name="amountColumn" Header="Amount of Events" Binding="{Binding Path=serverID, Mode= OneWay}"></DataGridTextColumn>
     </DataGrid.Columns>
    </DataGrid>       

如何显示这样的相同结果?以 10 的倍数为单位(以秒为单位的时间?)

<10 <20 <30 <40

并计算上述条件的事件数量?

我是否必须更改上面使用的 select 语句,以便它对例如 <20 的事件进行分组和计数?

4

2 回答 2

1

我没有尝试过,但试一试:

  var duration_query = this.db.Events
                       .Where(p => ID.Contains(p.ServerID) &&
                       p.Type == "Complete" && 
                      // fromDP and toDP are name of DataPicker control
                       (p.Date >= fromDP.SelectedDate.Value && 
                        p.Date <= toDP.SelectedDate.Value))
                       .GroupBy(p => (((int)(p.Duration / 10)) + 1)*10)
                       .Select(g => new
                       {
                       Duration = g.Key,
                       serverID = g.Count()
                       })
                       .OrderBy(x => x.Duration).ToList();

  dgProcessingTime.ItemsSource = duration_query.ToList();
于 2013-05-21T11:49:31.127 回答
1

我创建了一个示例代码,对范围之间的项目进行分组:

using System.Windows;
using System.Collections.Generic;
using System.Linq;
namespace TestWPFApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var eventList = new List<Event>();
            eventList.Add(new Event() { Duration = 9, Name = "Test", ServerId = 1 });
            eventList.Add(new Event() { Duration = 8, Name = "Test", ServerId = 2 });
            eventList.Add(new Event() { Duration = 5, Name = "Test", ServerId = 3 });
            eventList.Add(new Event() { Duration = 10, Name = "Test", ServerId = 4 });
            eventList.Add(new Event() { Duration = 12, Name = "Test", ServerId = 5 });
            eventList.Add(new Event() { Duration = 15, Name = "Test", ServerId = 6 });
            eventList.Add(new Event() { Duration = 20, Name = "Test", ServerId = 7 });
            eventList.Add(new Event() { Duration = 22, Name = "Test", ServerId = 8 });
            eventList.Add(new Event() { Duration = 23, Name = "Test", ServerId = 9 });
            eventList.Add(new Event() { Duration = 25, Name = "Test", ServerId = 10 });
            eventList.Add(new Event() { Duration = 30, Name = "Test", ServerId = 11 });

            var ceilings = new[] { 10, 20, 30, 40, 50, 60, 70, 80 };

            var grouped = eventList
                .GroupBy(item => ceilings.First(ceiling => ceiling > item.Duration))
                .Select(g => new { Duration = "<" + g.Key, serverID  = g.Count()});

            dgProcessingTime.ItemsSource = grouped.ToList();
        }
    }

    public class Event
    {
        public int Duration { get; set; }
        public string Name { get; set; }
        public int ServerId { get; set; }
    }
}

这将给出以下结果: 结果

您需要从GroupBy子句开始更改您发布的查询

于 2013-05-21T12:02:23.253 回答