Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 在asp.net中为每个对象数据绑定下拉列表VS_C#_Asp.net_Linq_Webforms - Fatal编程技术网

C# 在asp.net中为每个对象数据绑定下拉列表VS

C# 在asp.net中为每个对象数据绑定下拉列表VS,c#,asp.net,linq,webforms,C#,Asp.net,Linq,Webforms,填充下拉列表的哪种方法更有效 var info = db.table.Where( x => x.column == targetInformation). Select(x => new {x.item, x.index}); **************FOREACH foreach( var infos in info) { drowDownList.Items.Add(new {infos.item,infos.index.ToString()}

填充下拉列表的哪种方法更有效

var info = db.table.Where( x => x.column == targetInformation).
           Select(x => new {x.item, x.index});

**************FOREACH
foreach( var infos in info)
{
   drowDownList.Items.Add(new {infos.item,infos.index.ToString()});
}
**************DATABIND
dropDownList.DataSource = info;
dropDownList.DataBind();

这有点不同,但您可以尝试以下方式:

var results = table.AsEnumerable().Where(row => row.Field<int>("SomeType") > 1);

foreach (DataRow row in results)
{
    dropDownList.Items.Add(row.Field<string>("SomeText"), row.Field<int>("SomeID"));
}

//you don't need to databind if you're adding the items manually
//dropDownList.DataSource = info; 
//dropDownList.DataBind(); 
var results=table.AsEnumerable()。其中(row=>row.Field(“SomeType”)>1);
foreach(结果中的数据行)
{
dropDownList.Items.Add(row.Field(“SomeText”)、row.Field(“SomeID”);
}
//如果手动添加项目,则不需要进行数据绑定
//dropDownList.DataSource=info;
//dropDownList.DataBind();

如John Saunders所言,在考虑DB操作的情况下,这里的效率并不是真正值得考虑的问题。 换句话说,每一个的性能都将非常接近

<> P>你应该考虑的是,对于程序员来说,什么是更有效的阅读方式?另外,您是否将与团队中的其他人共享此代码?他们是更习惯于WPF风格的代码,还是更习惯于手工风格的代码


在这种情况下,最好的效率可能是最大限度地提高代码对您和您的同事的可读性。

是什么让您觉得有区别?此外,任何差异都会被这样一个事实所压倒,即您正在进行数据库I/O。如果一行的效率高于三行,那么
DataBind
方法的效率更高。但就性能而言,两者几乎相等,因为数据绑定无法创造奇迹。。。我指的是可读性和速度。我认为第二种方法看起来更干净,只是需要其他开发者的意见。