Data binding performance in ASP.Net web forms controls of entire class vs selection of property. Good practice?

StackOverflow https://stackoverflow.com/questions/22145711

Question

Is there any reason why you should do a select on a collection of objects, to retrieve only the desired property before setting the data source of a control such as a drop down?

for example if i have a class as seen below

 class CustomType 
 {
    public string Name{ get; set; }
    public bool isAuthorised{ get; set; }
    public bool isAdmin { get; set; }
 }

and a drop down that only need to display the names seen below.

<asp:DropDownList id="DropDownId" runat="server" DataTextField ="Name" 
DataValueField="Name">

Would it be better practice to bind the data like this where you perform a select for just the Name property.

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

vs Setting the entire List as the data source

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

I can't see any performance difference between the two, but was wondering if there is any benefit of selection only the relevant properties and what the standard practices for situations like this are.

No correct solution

OTHER TIPS

You'll notice the performance if you are selecting big data, for example I've Employees table with 25 records in it I run 2 queries and get the execution time:

Execution Time: 00:00:033

from e in Employees
select e

Execution Time: 00:00:002

from e in Employees
select e.EmployeeName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top