LINQ methods for translating from one sequence to another will not be equal. That is, the next test will fail if you used Cast()/ToList() .
Assert.AreSame(myDto.costPeriodList, myDto.periodCalcList);
In addition, using these methods means that if you try to add an item to one collection, they will not be reflected in another. And every time you called periodCalcList, it would create a completely new collection, which could be catastrophic depending on how many elements, how often this is called, etc.
The best solution, in my opinion, is not to use List<T> to store CostPeriodDto and instead use the collection obtained from Collection<T> and explicitly implement IEnumerable<IPeriodCalculation> . If you wish, you could implement IList<IPeriodCalculation> if you need to.
class CostPeriodDtoCollection : Collection<CostPeriodDto>, IEnumerable<IPeriodCalculation> { IEnumerable<IPeriodCalculation>.GetEnumerator() { foreach (IPeriodCalculation item in this) { yield return item; } } } class MyDto { public CostPeriodDtoCollection CostPeriods { get; set; } public IEnumerable<IPeriodCalculation> PeriodCalcList { get { return CostPeriods; } } }
Josh
source share