Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据条件在wpf datagrid中显示以下输出_C#_Wpf_Linq_Linq To Sql_Wpf Controls - Fatal编程技术网

C# 如何根据条件在wpf datagrid中显示以下输出

C# 如何根据条件在wpf datagrid中显示以下输出,c#,wpf,linq,linq-to-sql,wpf-controls,C#,Wpf,Linq,Linq To Sql,Wpf Controls,我在datagrid中显示处理时间数据,如下所示 使用以下语句 var duration_query = this.db.Events .Where(p => ID.Contains(p.ServerID) && p.Type == "Complete" && // fromDP and toDP are name

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

使用以下语句

  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();
var duration\u query=this.db.Events
.Where(p=>ID.Contains(p.ServerID)&&
p、 类型==“完成”和
//fromDP和toDP是数据采集器控件的名称
(p.Date>=fromDP.SelectedDate.Value&&
p、 日期(持续时间)
.选择(g=>new
{
持续时间=g键,
serverID=g.Count()
})
.OrderBy(x=>x.Duration).ToList();
dgProcessingTime.ItemsSource=duration_query.ToList();
XAML

我已经使用布局转换来旋转datagrid标头

    <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的倍数显示(时间以秒为单位?)


我还没试过,但试一下:

  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();
var duration\u query=this.db.Events
.Where(p=>ID.Contains(p.ServerID)&&
p、 类型==“完成”和
//fromDP和toDP是数据采集器控件的名称
(p.Date>=fromDP.SelectedDate.Value&&
p、 日期(((int)(p.Duration/10))+1)*10)
.选择(g=>new
{
持续时间=g键,
serverID=g.Count()
})
.OrderBy(x=>x.Duration).ToList();
dgProcessingTime.ItemsSource=duration_query.ToList();

我创建了一个示例代码,用于在以下范围内对项目进行分组:

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; }
    }
}
使用System.Windows;
使用System.Collections.Generic;
使用System.Linq;
命名空间TestWPFApp
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
var eventList=新列表();
添加(新事件(){Duration=9,Name=“Test”,ServerId=1});
添加(新事件(){Duration=8,Name=“Test”,ServerId=2});
添加(新事件(){Duration=5,Name=“Test”,ServerId=3});
添加(新事件(){Duration=10,Name=“Test”,ServerId=4});
添加(新事件(){Duration=12,Name=“Test”,ServerId=5});
添加(新事件(){Duration=15,Name=“Test”,ServerId=6});
添加(新事件(){Duration=20,Name=“Test”,ServerId=7});
添加(新事件(){Duration=22,Name=“Test”,ServerId=8});
添加(新事件(){Duration=23,Name=“Test”,ServerId=9});
添加(新事件(){Duration=25,Name=“Test”,ServerId=10});
添加(新事件(){Duration=30,Name=“Test”,ServerId=11});

var上限=新[{10,20,30,40,50,60,70,80}; var=eventList .GroupBy(项目=>上限.第一(上限=>上限>项目.持续时间))
.Select(g=>new{Duration=“那么,少于20个事件是否也会返回少于10个事件(累计总和)?对于您的更新,您是否可以解释一下(((int)(p.Duration/10))+1)*10)确实是这样吗?它是显示20到30之间的和,然后显示它,还是累计和小于30,比如说持续时间是
56
。它将被
10
,因此它将返回
5.6
,然后它将被转换为
int
,因此我们将以
5
结束。正如您所看到的那样,这将被截断最后一个数字。然后我们将
1
加到它上面,然后乘以
10
,最后我们得到
60
。它只显示小于
60
且大于或等于
50
的数字。您的意思是>=50之间的值,这是
50,不用担心。原因是当您将
60
除以
10
时>,你得到
6
,你把
1
加到它上面,得到
7
,然后乘以
10
,你会得到
70
。这都是关于组限制的。组被定义为小于
60
,因此
60
不小于
60
,因此最后是
小于70的组。var ceilings=new[]{10,20,30,40,50,60,70,80};您从代码隐藏中显式地给出它,但“时间以秒为单位”"在我的应用程序中,是在运行时生成的。如何在GroupBy中添加此项?您必须猜测最大可能的持续时间,并创建一个数组,该数组具有此方法的所有可能持续时间。如果没有问题,则可行。上限是我为保持一致性而硬编码的持续时间范围。实际上,您将初始化在运行时创建他的数组。