Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet as DataSource

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();
like image 712
Annie Avatar asked Oct 01 '12 14:10

Annie


1 Answers

I think this line could replace your second block of code:

CountryOptionsRepeater.DataSource = 
    countriesList
    .Distinct()
    .OrderBy(c => c)
    .Select(c => new { CountryName = c })
    .ToList();
like image 173
paul Avatar answered Nov 16 '22 21:11

paul