不包含 Where 和无扩展方法的定义?

Ken*_*ran 4 c# linq ienumerable

所以我环顾四周寻找我的问题的答案,我想添加using System.Linq,但我已经在那里添加了,所以我不知道我的代码没有编译什么。在我的代码上下文中,_accountreader存在,那么为什么它说不存在它的定义?

这条线return _accountReader.Where(x => x.Age);是编译器对我大喊大叫的地方。

public interface IAccountReader
{
    IEnumerable<Account> GetAccountFrom(string file);
}

public class XmlFileAccountReader : IAccountReader
{
    public IEnumerable<Account> GetAccountFrom(string file)
    {
        var accounts = new List<Account>();
        //read accounts from XML file
        return accounts;
    }
}

public class AccountProcessor
{
    private readonly IAccountReader _accountReader;
    public AccountProcessor(IAccountReader accountReader)
    {
        _accountReader = accountReader;
    }
    public IEnumerable<Account> GetAccountFrom(string file)
    {
        return _accountReader.Where(x => x.Age);
    }
}

public class Account
{
    public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 5

IAccountReader不执行IEnumerable<Account>

由于您提供了一种方法,因此GetAccountFrom您也可以使用它:

public IEnumerable<Account> GetAccountFrom(string file)
{
    return _accountReader.GetAccountFrom(file).Where(x => x.Age);
}
Run Code Online (Sandbox Code Playgroud)

除此之外Where是不正确的,您需要提供一个谓词,例如:

.Where(x => x.Age <= 10);
Run Code Online (Sandbox Code Playgroud)