Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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# 更好的解决方案是替换foreach循环或对其进行优化_C#_Silverlight - Fatal编程技术网

C# 更好的解决方案是替换foreach循环或对其进行优化

C# 更好的解决方案是替换foreach循环或对其进行优化,c#,silverlight,C#,Silverlight,this.entityModel.Entities是mydatagrid(agdatagrid)的源代码 我保留了AutoGenerateColumns=“False”。 我的agdatgrid 我希望第6列根据该列的数据可见。即,如果该列的任何一行包含数据,则该列应可见,如果该行中没有任何一行包含该列的数据,则该列应不可见 因此,我编写了一个foreach循环,但是如果数据很大,则加载ui需要更多的时间。那么还有别的办法吗 foreach (BrowserEntity _browseEntit

this.entityModel.Entities
是my
datagrid(agdatagrid)
的源代码

我保留了
AutoGenerateColumns=“False”
。 我的
agdatgrid

我希望第6列根据该列的数据可见。即,如果该列的任何一行包含数据,则该列应可见,如果该行中没有任何一行包含该列的数据,则该列应不可见

因此,我编写了一个foreach循环,但是如果数据很大,则加载ui需要更多的时间。那么还有别的办法吗

foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
            if (_browseEntity.State != null && this.entityModel.Entities.Count>0)
            {
                this.grid.DataSource = this.entityModel.Entities;
                this.grid.Columns[6].Visible = true;
                break;
            }
            else
            {

                this.grid.DataSource = this.entityModel.Entities;
                this.grid.Columns[6].Visible = false;
            }
}

我不是silverlight开发人员,但为什么要在foreach循环中检查“this.entityModel.Entities.Count>0”?我假设当您进入循环时计数总是>0,不是吗?

我不是silverlight开发人员,但是为什么您要在foreach循环中检查“this.entityModel.Entities.count>0”?我假设当你进入循环时,计数总是>0,不是吗?

我不是这方面的专家……但是为什么每次都要重置数据源呢

bool isColumnVisible = false;
this.grid.DataSource = this.entityModel.Entities;
foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
            if (_browseEntity.State != null && this.entityModel.Entities.Count>0)
            {
                isColumnVisible = true;
                break;
            }
}
this.grid.Columns[6].Visible = isColumnVisible;

我认为这应该更快…至少我希望如此。

我不是这方面的专家…但为什么每次都要重置数据源

bool isColumnVisible = false;
this.grid.DataSource = this.entityModel.Entities;
foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
            if (_browseEntity.State != null && this.entityModel.Entities.Count>0)
            {
                isColumnVisible = true;
                break;
            }
}
this.grid.Columns[6].Visible = isColumnVisible;

我认为这应该更快…至少我希望如此。

首先看看你所写内容的逻辑。您正在检查循环中的集合计数是否大于零;这将始终返回true,因为如果集合包含任何内容,循环将不会运行。因此,您实际编写的是这样的,当总是返回true或无法执行的代码被删除时:

foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
    if (_browseEntity.State != null)
    {
        this.grid.DataSource = this.entityModel.Entities;
        this.grid.Columns[6].Visible = true;
        break;
    }
}
因此,您多次分配数据源,并且从不将
Visible
设置为false,而我认为您实际尝试编写的内容是这样的:

// bind the grid but hide column 6
this.grid.DataSource = this.entityModel.Entities;
this.grid.Columns[6].Visible = false;

// if there is any state then show column 6
foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
     if (_browseEntity.State != null)
     {
         this.grid.Columns[6].Visible = true;
         break;
     }
}
或者,使用Linq,这可以写为以下内容,实现了相同的功能,但更加清晰:

this.grid.DataSource = this.entityModel.Entities;
this.grid.Columns[6].Visible = this.entityModel.Entities.Any(e => e.State != null);

首先看看你写的东西的逻辑。您正在检查循环中的集合计数是否大于零;这将始终返回true,因为如果集合包含任何内容,循环将不会运行。因此,您实际编写的是这样的,当总是返回true或无法执行的代码被删除时:

foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
    if (_browseEntity.State != null)
    {
        this.grid.DataSource = this.entityModel.Entities;
        this.grid.Columns[6].Visible = true;
        break;
    }
}
因此,您多次分配数据源,并且从不将
Visible
设置为false,而我认为您实际尝试编写的内容是这样的:

// bind the grid but hide column 6
this.grid.DataSource = this.entityModel.Entities;
this.grid.Columns[6].Visible = false;

// if there is any state then show column 6
foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
     if (_browseEntity.State != null)
     {
         this.grid.Columns[6].Visible = true;
         break;
     }
}
或者,使用Linq,这可以写为以下内容,实现了相同的功能,但更加清晰:

this.grid.DataSource = this.entityModel.Entities;
this.grid.Columns[6].Visible = this.entityModel.Entities.Any(e => e.State != null);

即使该列没有行数据,我也不想显示该列。因此,计数为0,并显示列。因此,为了避免这种情况,我使用了这个.entityModel.Entities.Count>0可能是Silverlight特定的,但是循环位于这个.entityModel.Entities集合的上方,并且在循环中检查这个集合的元素数量。我的理解是,它只会在实际存在元素时进入循环,因此检查.Count>0是多余的。虽然我在这里可能完全错了。我不想显示该列,即使该列没有行数据。因此,计数为0,并显示列。因此,为了避免这种情况,我使用了这个.entityModel.Entities.Count>0可能是Silverlight特定的,但是循环位于这个.entityModel.Entities集合的上方,并且在循环中检查这个集合的元素数量。我的理解是,它只会在实际存在元素时进入循环,因此检查.Count>0是多余的。虽然我在这里可能完全错了。我将使用linq查询,因为我对linq查询的理解将比foreach循环更快。。感谢您的精彩回答,我将使用linq查询,据我所知,linq查询将比foreach循环更快。。谢谢你的回答