Naor
Naor

Reputation: 24103

error when create an expression

I have this code:

public class InputMapper : BaseMapper<Input, InputDTO>
{
    private Guid _CompanyId;
    public InputMapper(Guid companyId)
    {
        _CompanyId=companyId;
    }

    public override Expression<Func<InputDTO, Input>> ToDomain()
    {
        return x=> new Input()
        {
            CompanyId => this._CompanyId, <--- HERE I GET AN ERROR
            Id = x.Id,
            Name = x.Name,
            Deduction = x.Deduction
        };
    } 
}

Why do I get error on the marked line:

Invalid initializer member declarator

?

Is there any workaround?

Upvotes: 0

Views: 341

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1504092

You've used a lambda expression, where I suspect you meant to just initialize a property:

 CompanyId => this._CompanyId,

should be

 CompanyId = this._CompanyId,

(Also note Brandon's comment - the assignment in your constructor is the wrong way round.)

Upvotes: 3

Andras Zoltan
Andras Zoltan

Reputation: 42363

I think it should just be

return x=> new Input()
{ CompanyID = this._CompanyID, ....

You're don't want assign a lambda to the returned object's CompanyID member.

Upvotes: 1

Related Questions