1

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());
    }
3
  • Look at the signature for IndexOf to see what it actually accepts. Commented Aug 23, 2017 at 17:14
  • @Servy It accepts (TEntity item) Commented Aug 23, 2017 at 17:19
  • And is that lambda of type TEntity? Commented Aug 23, 2017 at 17:20

2 Answers 2

4

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));
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you are missing one these references, Try adding them

using System.Linq;

using System.Data.Entity;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.