I've added this inline code block to Sharepoint page.
I know the Web front end where it's running has .NET 3.0
<script runat="server">
protected void Page_PreRender(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Facilities"];
SPField field = list.Fields["Title"];
List<string> Titles = new List<string>();
foreach(SPListItem spli in list.Items)
{
Titles.Add(spli[field.Id].ToString());
}
Titles.Sort();
Titles.Distinct().ToList().ForEach(delegate(string code)
{
Facilities.Items.Add(new ListItem(code));
});
}
</script>
I'm getting this error:
'System.Collections.Generic.List<string>' does not contain a
definition for 'Distinct'
Thanks for any help or information.
> [...]
> I'm getting this error:
>
> 'System.Collections.Generic.List<string>' does not contain a
> definition for 'Distinct'
You need to compile with C# 3.0, against the .NET 3.5 libraries. The
Distinct() method you seem to be trying to use is one of the LINQ
extension methods. You need C# 3.0 for extension method support, and .NET
3.5 to get the LINQ Enumerable class.
Pete
You will need .NET 3.5 and you will need to tell your web app
to use 3.5 when compiling.
You can add something like:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
compilerOptions="/warnaserror-" warningLevel="4"
type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript"
extension=".vb" compilerOptions="/optioninfer+"
type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
</compiler>
</compilers>
</system.codedom>
to web.config.
Arne
I was going to code a work around, but have an issue where I can't
even find my dropdownlist from my code block.. and SPD is not easy
for troubleshooting.
I've tried OnInit, OnLoad and OnPrerender.
protected void Page_OnInit(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Facilities"];
SPField field = list.Fields["Title"];
List<string> Titles = new List<string>();
foreach(SPListItem spli in list.Items)
{
DropDownList fddl = (DropDownList)this.Page.FindControl
("FacilityDDL");
fddl.Items.Add("x");
// if (!fddl.Items.Contains(spli[field.Id].ToString()))
//if (fddl.Items.IndexOf(fddl.Items.FindByValue(spli
[field.Id].ToString())) =-1)
//{
//response.write("x");
// fddl.Items.Add(spli[field.Id].ToString());
//}
}
}
</script>
my asp.net control is inside:
<WebPartPages:DataFormWebPart
<asp:DropDownList runat="server" id="FacilityDDL"
DataTextField="Title" DataValueField="Title" /></td>
Am I missing something here?