Travis
Travis

Reputation: 23

Cannot convert lambda expression to type because it is not a delegate

I'm trying to create an Index of List. But, for some reason I keep getting the following error:

Cannot convert lambda expression to type 'TEntity' because it is not a delegate

I have tried searching quite a bit on the issue, but everything I've tried doesn't seem to work. I've tried everything from creating a cast to assuring I have the correct Using statements

This is the line that is specifically throwing the error

int index = items.IndexOf(x => ID(x) == id.Value);

Which is calling this method

protected Int32? ID(TEntity entity)
{
    return entity.As<dynamic>().__id;
}

For reference, the rest of what's applicable is here.

[Element("<div/>"), Editor, IdProperty("__id")]
public abstract class GridEditorBase<TEntity> : EntityGrid<TEntity>, ISetEditValue, IGetEditValue
    where TEntity : class, new()
{
    private int nextId = 1;

    public GridEditorBase(jQueryObject container)
        : base(container)
    {
    }

    protected Int32? ID(TEntity entity)
    {
        return entity.As<dynamic>().__id;
    }

    protected virtual void Save(ServiceCallOptions opt, Action<ServiceResponse> callback)
    {
        SaveRequest<TEntity> request = opt.Request.As<SaveRequest<TEntity>>();
        TEntity row = Q.DeepClone(request.Entity);
        int? id = row.As<dynamic>().__id;
        if (id == null)
            row.As<dynamic>().__id = nextId++;

        if (!ValidateEntity(row, id))
            return;

        List<TEntity> items = view.GetItems().Clone();

        if (id == null)
            items.Add(row);
        else
        {                
            int index = items.IndexOf(x => ID(x) == id.Value);
            items[index] = Q.DeepExtend<TEntity>(new TEntity(), items[index], row);
        }

        SetEntities(items);
        callback(new ServiceResponse());
    }

Upvotes: 1

Views: 2499

Answers (2)

Mohamed Ziham
Mohamed Ziham

Reputation: 58

Maybe you are missing one these references, Try adding them

using System.Linq;

using System.Data.Entity;

Upvotes: 0

oerkelens
oerkelens

Reputation: 5161

You need to pass an item to IndexOf, which you could do like this:

int index = items.IndexOf(items.FirstOrDefault(x => ID(x) == id.Value));

Upvotes: 4

Related Questions