I am trying to optimize the code for SharePoint webpart. I have a repeater control:
<asp:Repeater ID="CountryOptionsRepeater" runat="server">
<ItemTemplate>
<option value='<%#Eval("CountryName") %>'><%#Eval("CountryName") %></option>
</ItemTemplate>
</asp:Repeater>
I am filling it with data-table
countriesList = countriesList.Distinct<String>().ToList<String>();
countriesList.Sort();
//var noDupsCountriesList = new HashSet<String>(countriesList);
DataTable dt = new DataTable();
dt.Columns.Add("CountryName");
foreach (String countryName in countriesList)
{
DataRow dr = dt.NewRow();
dr["CountryName"] = countryName;
dt.Rows.Add(dr);
}
CountryOptionsRepeater.DataSource = dt;
CountryOptionsRepeater.DataBind();
this.DataBind();
Is there a way to directly bind HashSet object (noDupsCountriesList) to DataSource with same configuration of repeater, in order to bring about optimization?
Something like:
//countriesList = countriesList.Distinct<String>().ToList<String>();
//countriesList.Sort();
var noDupsCountriesList = new HashSet<String>(countriesList);
CountryOptionsRepeater.DataMember = "CountryName"; // ??
CountryOptionsRepeater.DataSource = noDupsCountriesList;
CountryOptionsRepeater.DataBind();
this.DataBind();
I think this line could replace your second block of code:
CountryOptionsRepeater.DataSource =
countriesList
.Distinct()
.OrderBy(c => c)
.Select(c => new { CountryName = c })
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With