Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Lamba support in WebForms ASCX

Tags:

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?

like image 689
tags2k Avatar asked Jul 02 '09 12:07

tags2k


1 Answers

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?

like image 164
JaredPar Avatar answered Oct 12 '22 07:10

JaredPar