Sto cercando di ottimizzare il codice per la web part SharePoint. Ho un controllo ripetitore:HashSet as DataSource
<asp:Repeater ID="CountryOptionsRepeater" runat="server">
<ItemTemplate>
<option value='<%#Eval("CountryName") %>'><%#Eval("CountryName") %></option>
</ItemTemplate>
</asp:Repeater>
sto riempiendolo con i dati-tavolo
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();
Esiste un modo per legare direttamente HashSet oggetto (noDupsCountriesList) per DataSource con la stessa configurazione del ripetitore, al fine per ottenere l'ottimizzazione?
Qualcosa di simile:
//countriesList = countriesList.Distinct<String>().ToList<String>();
//countriesList.Sort();
var noDupsCountriesList = new HashSet<String>(countriesList);
CountryOptionsRepeater.DataMember = "CountryName"; // ??
CountryOptionsRepeater.DataSource = noDupsCountriesList;
CountryOptionsRepeater.DataBind();
this.DataBind();
Perché è necessario DataTable o HashSet? 'CountryOptionsRepeater.DataSource = countriesList;' Non fare il trucco? – paul
Potrebbe essere possibile eseguire il binding direttamente a 'HashSet <>', ma non ti darà i Paesi in ordine alfabetico, che dal tuo codice sembra essere necessario. – MiMo
@MiMo, grazie aggiungerei l'istruzione 'OrderBy' (supponendo che' initializaiton + OrderBy' di HashSet sia molto più ottimizzato di Call List <> 's Distinct() + ToList() + Sort() ') .. – Annie