我在数据网格中显示处理时间数据,如下所示
使用以下声明
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
我使用了Layout Transform来旋转数据网格标题。
<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的倍数(以秒为单位的时间?)
&lt; 10&lt; 20&lt; 30&lt; 40
并计算上述条件的事件数量?
我是否必须更改上面使用过的select语句,以便对事件数量进行分组和计算,例如<?p?
答案 0 :(得分: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();
答案 1 :(得分: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
子句开始更改您发布的查询