C# ASP.Net web窗体控件中的数据绑定性能—整个类的控件与属性的选择。良好做法?
在设置控件(如下拉列表)的数据源之前,是否有任何理由要对对象集合执行选择,以仅检索所需的属性 例如,如果我有一个如下所示的类C# ASP.Net web窗体控件中的数据绑定性能—整个类的控件与属性的选择。良好做法?,c#,asp.net,linq,webforms,web-controls,C#,Asp.net,Linq,Webforms,Web Controls,在设置控件(如下拉列表)的数据源之前,是否有任何理由要对对象集合执行选择,以仅检索所需的属性 例如,如果我有一个如下所示的类 class CustomType { public string Name{ get; set; } public bool isAuthorised{ get; set; } public bool isAdmin { get; set; } } 还有一个下拉列表,只需要显示下面看到的名称 <asp:DropDownList id
class CustomType
{
public string Name{ get; set; }
public bool isAuthorised{ get; set; }
public bool isAdmin { get; set; }
}
还有一个下拉列表,只需要显示下面看到的名称
<asp:DropDownList id="DropDownId" runat="server" DataTextField ="Name"
DataValueField="Name">
最好的做法是像这样绑定数据,只对Name属性执行select
List<CustomType> customType = GetCustomTypeList();
DropDown1.Datasource = customType.Select(c => c.Name);
DropDown1.DataBind():
List customType=GetCustomTypeList();
DropDown1.Datasource=customType.Select(c=>c.Name);
DropDown1.DataBind():
vs将整个列表设置为数据源
List<CustomType> customType = GetCustomTypeList();
DropDown1.Datasource = customType;
DropDown1.DataBind():
List customType=GetCustomTypeList();
DropDown1.Datasource=customType;
DropDown1.DataBind():
我看不出两者在性能上有什么不同,但我想知道只选择相关属性是否有任何好处,以及这种情况下的标准做法是什么。如果选择大数据,您会注意到性能,例如,我有一个包含25条记录的Employees表,我运行了2个查询并获得了执行时间: 执行时间:00:00:033 执行时间:00:00:002
from e in Employees
select e
from e in Employees
select e.EmployeeName