I am trying to convert my class to a IEnumerable object.
My class is:
public class StageTwo : IEnumerable
{
public string Party { get; set; }
public string Currency { get; set; }
public string DrCr { get; set; }
public string Account { get; set; }
public double? Amount { get; set; }
public IEnumerator GetEnumerator()
{
return GetEnumerator();
}
}
Below I have a method where I query some data and place it successfully into my StageTwo object. Then I want to duplicate most of the data and changes -item.Amount
.
public IEnumerable<StageTwo> Stage2Entries()
{
var queryJoin = (from inn in db.Input.Take(1)
join y in db.InputY on inn.YAction equals y.YAction
orderby inn.Id descending
select new StageTwo
{
Id = inn.Id,
Party = inn.XParty,
Currency = inn.Curr,
DrCr = y.DrAccount1,
Account = y.YAction,
Amount = inn.Amount
});
/*WORKS FINE ABOVE*/
StageTwo stageTwoFinal = new StageTwo();
foreach (var item in queryJoin)
{
stageTwoFinal.Id = item.Id;
stageTwoFinal.Party = item.Party;
stageTwoFinal.Currency = item.Currency;
stageTwoFinal.DrCr = item.DrCr;
stageTwoFinal.Account = item.Account;
stageTwoFinal.Amount = item.Amount;
stageTwoFinal.Id = item.Id;
stageTwoFinal.Party = item.Party;
stageTwoFinal.Currency = item.Currency;
stageTwoFinal.DrCr = item.DrCr;
stageTwoFinal.Account = item.Account;
stageTwoFinal.Amount = -item.Amount;
}
/*Unable to convert here*/
var collection = (IEnumerable<StageTwo>)stageTwoFinal;
return stageTwoFinal;
}
I am getting a System.InvalidCastException
How can I convert this class to an IEnumerable?