Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does not contain a definition for Where and no extension method?

So I've looked around for awhile for the answer to my problem, and I'm seeing to add using System.Linq, except I already have that there so I don't know what my code isn't compiling. In my context of code, _accountreader exists, so why is it saying no definition of it exists?

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; }
}
like image 715
Kennedy Tran Avatar asked Mar 20 '23 16:03

Kennedy Tran


2 Answers

IAccountReader does not implement IEnumerable<Account>.

Since you have provided a method GetAccountFrom you could also use this:

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

Apart from that the Where is incorrect, you need to provide a predicate, for example:

.Where(x => x.Age <= 10);
like image 180
Tim Schmelter Avatar answered Apr 26 '23 03:04

Tim Schmelter


You can add System.Linq namespace. Since where has been developed under this namespace, it should be added.

like image 37
Kanimozhi Avatar answered Apr 26 '23 01:04

Kanimozhi