C# displaying error 'Delegate 'System.Func<...>' does not take 1 arguments

21,893

Solution 1

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.

Solution 2

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)

Solution 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.

Share:
21,893
Harold
Author by

Harold

Full stack developer at Cronofy working in Ruby on Rails, PHP, C#, JavaScript, CSS, and HTML. Previously a full stack developer at UNiDAYS working in C#, JavaScript, SQL Server, and ASP.net/Razor. I have university experience of working in C++ with various different graphical engines, years of freelance experience working in PHP, Flash, JavaScript, and MySQL, and hobby experience making games in JavaScript, HTML5 and Phaser.io. Hobby interests include League of Legends, Game of Thrones, Pokemon, walking, and memes.

Updated on June 26, 2020

Comments

  • Harold
    Harold almost 4 years

    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?