整个类与属性选择 ASP.Net Web 表单控件中的数据绑定性能.良好做法



有什么理由应该对对象集合进行选择,以便在设置控件的数据源(如下拉列表)之前仅检索所需的属性?

例如,如果我有一个如下所示的类

 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 属性执行选择。

 List<CustomType> customType = GetCustomTypeList();
 DropDown1.Datasource = customType.Select(c => c.Name);
 DropDown1.DataBind():

vs 将整个列表设置为数据源

 List<CustomType> customType = GetCustomTypeList();
 DropDown1.Datasource = customType;
 DropDown1.DataBind():

我看不出两者之间的任何性能差异,但想知道仅选择相关属性是否有任何好处,以及这种情况的标准做法是什么。

如果您选择大数据,您会注意到性能,例如,我有 员工表 其中有 25 条记录 我运行 2 个查询并获取执行时间:

执行时间:00

:00:033

from e in Employees
select e

执行时间:00:00:002

from e in Employees
select e.EmployeeName

最新更新