C# 从母类实现非泛型IEnumerable

C# 从母类实现非泛型IEnumerable,c#,ienumerable,C#,Ienumerable,我想就我面临的一个问题向专家们请教。在自底向上的方法中,我有一个类StandardAccount,用几个简单的属性初始化,其中一个是enum(和一个在实例化时自动设置的GUID,如 public class StandardAccount { private Guid _id; private string _accName; private AccountType _accType; private double _balance = 0; public

我想就我面临的一个问题向专家们请教。在自底向上的方法中,我有一个类StandardAccount,用几个简单的属性初始化,其中一个是enum(和一个在实例化时自动设置的GUID,如

public class StandardAccount
{
    private Guid _id;
    private string _accName;
    private AccountType _accType;
    private double _balance = 0;
    public enum AccountType
    {
        [Description("Asset")]
        AT,
        [Description("Liability")]
        LY,
        [Description("Profit")]
        PT,
        [Description("Loss")]
        LS
    }

    public StandardAccount(string name, AccountType type)
    {
        this._id = Guid.NewGuid();
        this._accName = name;
        this._accType = type;
        this.Balance = 0;
    }
    public double Balance { get => _balance; set => _balance = value; }
}
Book类必须有一个或多个StandardAccounts列表,Accounting类必须有多个Books。我以可搜索的方式准备Book类(将有多个StandardAccount列表,稍后我需要能够通过这些列表中的GUID找到StandardAccount)。我按如下方式设置Book类:

public class Book
{
    private string _bookName;
    private short _bookNum;
    private Guid _bookId;

    public List<StandardAccount> Accounts { get; set; }
    public IEnumerator<StandardAccount> GetEnumerator() => Accounts.GetEnumerator();

    //compile time err: containing type does not implement interface 'IEnumerable'
    IEnumerator IEnumerable.GetEnumerator() => Accounts.GetEnumerator();
    
    //updated
    public void Add(string name, StandardAccount.AccountType type) => Accounts.Add(new StandardAccount(name, type));

    public Book(Guid? id, short BookNumber, string BookName, IEnumerable<StandardAccount> Account)
    {
        this._bookId = id ?? Guid.NewGuid();
        this._bookNum = BookNumber;
        this._bookName = BookName;
        this.Accounts = new List<StandardAccount>();
    }
    public Guid id { get => _bookId; set => _bookId = value; }
}
我正在尝试这样做:

//this works fine
StandardAccount stdAccount = new StandardAccount("account one", StandardAccount.AccountType.AT);
//this workds fine
stdAccount.Balance = 123;

//but.. cannot add the account to my book
//ERROR - System.NullReferenceException: 'Object reference not set to an instance of an object.'
myAccounting.Book.Accounts.Add(stdAccount);
这意味着您正在使用。但是,book类没有实现IEnumerable接口,因此会导致错误

解决方案是要么实现接口

public class Book : IEnumerable
请注意,这需要更改名称,因为您已经有了一个具有相同签名的方法

public IEnumerator GetNonGenericEnumerator() => Accounts.GetEnumerator();
然而,正如评论中提到的。这可能不是解决问题的好方法。例如,你应该使用组合而不是继承,即“一本书有一个账户列表”,而不是“一本书就是一个账户列表”

从问题来看,您想要搜索的内容和期望的结果并不明显。如果您想要搜索一本书,您可以使用以下帐户:

myBooks.Where(b => b.Accounts.Any(a => a.Id == idToSearchFor));
myBooks.SelectMany(b => b.Accounts).Where(a => a.Id == idToSearchFor);
但您需要公开Id。如果您希望返回实际帐户,您可以使用:

myBooks.Where(b => b.Accounts.Any(a => a.Id == idToSearchFor));
myBooks.SelectMany(b => b.Accounts).Where(a => a.Id == idToSearchFor);

为什么您需要该功能?如果您需要迭代帐户,请执行以下操作:
foreach(book.accounts中的var帐户)
一本书不是作者的集合。它有作者。如果有一个
GetEnumerator
返回一个不相关的实体,会让每个人都感到困惑,甚至是作者(您)过了一会儿,你为什么要首先实现
IEnumerable
?你的
List
已经有了这些功能。对于另一个错误,你需要添加一个
Account
的新实例,而不仅仅是两个属性。但同样,不需要该方法,因为
List
属性已经有了它。@Nick什么一种方法是,将
Book
视为
Author
对象的容器是非常不寻常的,而且几乎总是错误的。如果你需要在书或章节中添加标签和关键字,该怎么办?为什么迭代一本书会返回作者而不是章节?非常感谢!你是对的,我澄清:我尝试做
StandardAccount stdAccount=new standardcount(“account1”,standardcount.AccountType.AT);
然后简单地
Book.Accounts.Add(stdAccount);
是的,正如您所写的,还有其他人,一本书有(不是)多个帐户。我尝试在一本书中逐个列表搜索一个具有特定(事先知道)GUID id的帐户。@everybody