Does not contain a definition of the Where and no extension method?

So, I looked around for a while to answer my problem, and I see to add using System.Linq, except that I already have this, so I don’t know that my code does not compile. There is code in my context _accountreader, so why does it say that the definition of this does not exist?

The line return _accountReader.Where(x => x.Age);is where the compiler yells at me.

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; }
}
+3
source share
3 answers

IAccountReaderdoes not implement IEnumerable<Account>.

Since you provided a method GetAccountFrom, you can also use this:

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

Also, Whereincorrectly, you need to provide a predicate, for example:

.Where(x => x.Age <= 10);
+4
source

System.Linq. , , .

+2

I assume you wanted to implement this:

public class AccountProcessor
{    
    // ...

    public IEnumerable<int> GetAgesFrom(string file)
    {
        return _accountReader.GetAccountFrom(file).Where(x => x.Age);
    }
}
0
source

All Articles