HashSet as DataSource
.net, asp.net, c#, sharepoint
Solution
I think this line could replace your second block of code:
CountryOptionsRepeater.DataSource =
countriesList
.Distinct()
.OrderBy(c => c)
.Select(c => new { CountryName = c })
.ToList();
Problem
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(); ```