【问题标题】:Cannot convert lambda expression to type because it is not a delegate无法将 lambda 表达式转换为类型,因为它不是委托
【发布时间】:2017-08-23 17:08:25
【问题描述】:

我正在尝试创建列表索引。但是,由于某种原因,我不断收到以下错误:

无法将 lambda 表达式转换为类型“TEntity”,因为它不是 委托

我已尝试在此问题上进行大量搜索,但我尝试过的所有方法似乎都不起作用。从创建演员表到确保我有正确的Using 语句,我已经尝试了所有方法

这是专门抛出错误的行

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

哪个调用了这个方法

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

作为参考,其余适用的内容都在这里。

[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());
    }

【问题讨论】:

  • 查看IndexOf 的签名以了解它实际接受的内容。
  • @Servy 它接受(TEntity 项)
  • 那是TEntity类型的lambda吗?

标签: c# lambda delegates


【解决方案1】:

您需要将一个项目传递给IndexOf,您可以这样做:

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

【讨论】:

    【解决方案2】:

    也许您缺少这些参考资料,请尝试添加它们

    使用 System.Linq;

    使用 System.Data.Entity;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多