1
public class EcImageWrapper
{

    //etc...

    public IQueryable<EcFieldWrapper> ListOfFields
    {
        get
        {
            //logic here

            return this.listOfFields.AsQueryable();
        }
    }

    public EcFieldWrapper FindBy(Expression<Func<EcFieldWrapper, int, bool>> predicate)
    {
        return this.ListOfFields.Where(predicate).SingleOrDefault();
    }

    public EcFieldWrapper FindByName(string fieldName)
    {
        return this.FindBy(x => x.Name.ToUpper() == fieldName.ToUpper());
        //error here, wanting 3 arguments
        //which makes sense as the above Expression has 3
        //but i don't know how to get around this
    }

出于某种原因,Expression> 要求我使用 3 个参数,过去我只对有问题的实例使用了 2 个。但是,现在我想通过这个实例中的集合进行查找。

我收到以下错误:

Delegate 'System.Func<MSDORCaptureUE.Wrappers.EcFieldWrapper,int,bool>' does not take 1 arguments
The best overloaded method match for 'CaptureUE.Wrappers.EcImageWrapper.FindBy(System.Linq.Expressions.Expression<System.Func<CaptureUE.Wrappers.EcFieldWrapper,int,bool>>)' has some invalid arguments
Argument 1: cannot convert from 'string' to 'System.Linq.Expressions.Expression<System.Func<CaptureUE.Wrappers.EcFieldWrapper,int,bool>>'

期望的结果:能够在类 EcImageWrapper 的实例中使用返回类型为 EcFieldWrapper 的谓词和 EcFieldWrapper 的谓词类型

为什么需要3?为什么是int?

4

2 回答 2

5

现有方法不需要 3 个参数 - 它需要一个,Expression<Func<EcFieldWrapper, int, bool>>因为这是您声明要接受的方法。那将是这样的:

// This version ignores the index, but you could use it if you wanted to.
(value, index) => value.Name.ToUpper() == fieldName.ToUpper()

index是集合中的位置,因为您称其Queryable.Where.

假设您不需要索引,我强烈怀疑您只想将FindBy方法更改为:

public EcFieldWrapper FindBy(Expression<Func<EcFieldWrapper, bool>> predicate)
{
    return this.ListOfFields.Where(predicate).SingleOrDefault();
}

或者更简单地说,使用SingleOrDefault接受谓词的重载:

public EcFieldWrapper FindBy(Expression<Func<EcFieldWrapper, bool>> predicate)
{
    return this.ListOfFields.SingleOrDefault(predicate);
}

这应该可以正常工作 - 如果没有,您应该告诉我们当您尝试时会发生什么。

于 2013-02-07T17:25:25.737 回答
0

不需要Find方法。做所有事情FindByName

public EcFieldWrapper FindByName(string fieldName)
{
    fieldName = fieldName.ToUpper();
    return ListOfFields
       .SingleOrDefeault(x => x.Name.ToUpper() == fieldName);
}
于 2013-02-07T17:31:47.440 回答