18

I am calling:

 form = new FormFor<Project>() .Set(x => x.Name, "hi"); 

where Project has a field called Name and FormFor's code is:

public class FormFor<TEntity> where TEntity : class { FormCollection form; public FormFor() { form = new FormCollection(); } public FormFor<TEntity> Set(Expression<Func<TEntity>> property, string value) { form.Add(property.PropertyName(), value); return this; } } 

but it keeps telling me Delegate 'System.Func<ProjectSupport.Core.Domain.Project>' does not take 1 arguments and I am unsure why. Could anyone shed some light on it for me?

3 Answers 3

23

It's trying to convert this lambda expression:

x => x.Name 

into an Expression<Func<TEntity>>.

Let's ignore the expression tree bit for the moment - the delegate type Func<TEntity> represents a delegate which takes no arguments, and returns a TEntity. Your lambda expression x => x.Name clearly is expecting a parameter (x). I suspect you want

Expression<Func<TEntity, string>> 

or something similar, but it's not really clear what you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

5

Type of expression "x => x.Name" is not Expression<Func<TEntity>>, but Expression<Func<TEntity, string>>. I suppose, you should change declaration of Set method:

public FormFor<TEntity> Set<V>(Expression<Func<TEntity, V>> property, string value) 

Comments

3

Func<TEntity> is a delegate taking zero parameters and returns an object of type TEntity. You are trying to supply an x and return nothing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.