views:

304

answers:

1

After having worked in MVC for a few months, I'm back in a previously written WebForms 3.5 application, and I'm trying to fix up what I can with what I've learned.

Part of this is the "strongly-typed model with a partial view" concept which is incredibly awesome. By inheriting my custom "ListTemplate" control, I can then use its GetModel() method to get something resembling this:

<% List<Models.CaseStudy> model = GetModel<Models.CaseStudy>(); %>

I can then run a foreach over model, and all is happy. However, I wanted to do a grouping so I added references to:

<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Linq.Expressions" %>

Then, with a slightly less-than-ideal syntax, tried this:

<% IEnumerable<IGrouping<string, Models.CaseStudy>> model = GetModel<Models.CaseStudy>().GroupBy(e => e.Client.Name); %>

But no! "Compiler Error Message: CS1525: Invalid expression term '>'" - and it appears to be the lambda at fault. It doesn't work if I put the GroupBy() in the foreach parameters either.

Is there any way to get lambdas working within ASCX files?

+2  A: 

Based on the error message, my first instinct would be to say that you are using the C# 2.0 compiler.

I'm not sure what the return type of GetModel<T> is. But even if it's an empty method, the actual lambda expression is a valid syntactical construct. It should produce an overload resolution error, not a parsing error.

However this is not the case in the C# 2.0 compiler. It would be an invalid syntactical construct and would produce that error message.

Can you check to make sure you are using the correct version of the C# compiler?

JaredPar
Ah, it does say: Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053. How can I specify v3.5, and why would ASCX files process differently from the rest of my site (which is definitely v3.5 as it set as the target framework in the web project properties pane)?
tags2k
As an aside, the return type of GetModel<T> is List<T>
tags2k
Sorted it now - that's weird. Set up a new 3.5 project to compare the web.config files and for some reason the system.codedom > compilers section was empty from my other project! I wonder how long it's been like that!
tags2k