Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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# 每日每小时发布的LINQ声明_C#_Linq - Fatal编程技术网

C# 每日每小时发布的LINQ声明

C# 每日每小时发布的LINQ声明,c#,linq,C#,Linq,我有以下语句,需要很长时间才能加载。有人能告诉我如何解决这个性能问题,并且每小时都能得到相同的结果吗。我必须先循环每台机器,然后循环每台机器的每一小时 foreach (string MachineID in this.lboxMachines.SelectedItems) { if (this.lboxMachines.SelectedItems.Contains(GimaID)) { { for (int i = 0;

我有以下语句,需要很长时间才能加载。有人能告诉我如何解决这个性能问题,并且每小时都能得到相同的结果吗。我必须先循环每台机器,然后循环每台机器的每一小时

foreach (string MachineID in this.lboxMachines.SelectedItems)
{
    if (this.lboxMachines.SelectedItems.Contains(GimaID))
    {
        {        
            for (int i = 0; i <= 23; i++)
            {

                var PartsCast = (from p in ProductionEntity.PARTDATAs
                                 where p.DATE_TIME >= StartDate
                                 where p.DATE_TIME <= EndDate
                                 where p.MACHINE == MachineID
                                 select p).Count();

                StartDate.AddHours(1);

                DT.Rows[row][col] = PartsCast;
                col++;
            }
        }
    }
}
foreach(此.lboxMachines.SelectedItems中的字符串MachineID)
{
if(this.lboxMachines.SelectedItems.Contains(GimaID))
{
{        
对于(int i=0;i=StartDate)

其中p.DATE\u TIME基于您的代码,请尝试以下操作

    if (this.lboxMachines.SelectedItems != null && this.lboxMachines.SelectedItems.Contains(GimaID))
    {                                           
        foreach (string MachineID in this.lboxMachines.SelectedItems)
        {
            for (int i = 0; i <= 23; i++)
            {
                var PartsCast = (from p in ProductionEntity.PARTDATAs
                         where p.DATE_TIME >= StartDate
                         where p.DATE_TIME <= EndDate
                         where p.MACHINE == MachineID
                         select p).Count();

                StartDate = StartDate.AddHours(1);

                DT.Rows[row][col] = PartsCast;
                col++;
            }
        }
    }
if(this.lboxMachines.SelectedItems!=null&&this.lboxMachines.SelectedItems.Contains(GimaID))
{                                           
foreach(此.lboxMachines.SelectedItems中的字符串MachineID)
{
对于(int i=0;i=StartDate)

其中p.DATE\u TIME我相信,由于Linq的IQueryable性质,您正在让代码多次获取内容,这将导致速度减慢。让我们将其分解为多个步骤,看看是否可以从中吸取教训

在下面的例子中,我忽略了数据的去向,只给出了所需的处理和提取信息的结构

// Get the machines to process only once by not getting a queryable.
var machines =
this.lboxMachines.SelectedItems
                 .Where( machine => machine.Contains(GimaID) )
                 .ToList(); // Don't keep this IQueryable but as a hard list by this call.

// Get *only* the parts to use; using one DB call
var parts = ProductionEntity.PARTDATAs
                            .Where(part => machines.Contains(part.Machine))
                            .ToList(); 

// Now from the parts get the count based off of the time each hour
var resultPerHour = 
      Enumerable.Range(0, 24)
                .Select (hour => new
                                {
                                   Hour = hour,
                                   Count = parts.Count(part => part.DATETIME >= StartDate.AdHours(hour) && part.DATETIME <= EnDate)
                                 }); 
//通过不获取可查询项,仅获取要处理的计算机一次。
var机器=
this.lboxMachines.SelectedItems
.Where(machine=>machine.Contains(GimaID))
.ToList();//不要将此IQueryable保留为此调用的硬列表。
//获取*仅*要使用的部分;使用一个DB调用
var parts=ProductionEntity.PARTDATAs
.Where(part=>machines.Contains(part.Machine))
.ToList();
//现在从部件中获取基于每小时时间的计数
var resultPerHour=
可枚举范围(0,24)
.选择(小时=>新建)
{
小时=小时,

Count=parts.Count(part=>part.DATETIME>=StartDate.AdHours(hour)和&part.DATETIME您可以通过doing一次查询所有内容。其中(p=>p.DATE\u TIME>=StartDate和p.DATE\u TIME p.DATE\u TIME.hour)

StartDate.AddHours(1);
不会在
StartDate
中递增,您需要像
StartDate=StartDate.AddHours(1)一样重新分配结果
为什么对每个选定的
机器执行24次循环ID
?我需要在每天24小时内的每个小时之间进行计数,有没有更好的方法可以做到这一点?快速回答…您可以获得满足您条件的所有
零件数据,然后按
日期进行分组,最后按
Enumera退出联接ble.Range(1,23);
如果
EndDate
为常数,则“每小时”的结果不会如您所愿。