C# 如何使一个泛型列表等于另一个泛型列表

C# 如何使一个泛型列表等于另一个泛型列表,c#,C#,这是我的设置 class CostPeriodDto : IPeriodCalculation { public decimal? a { get; set; } public decimal? b { get; set; } public decimal? c { get; set; } public decimal? d { get; set; } } interface IPeriodCalculation { decimal? a { get;

这是我的设置

class CostPeriodDto : IPeriodCalculation
{
    public decimal? a { get; set; }
    public decimal? b { get; set; }
    public decimal? c { get; set; }
    public decimal? d { get; set; }
}

interface IPeriodCalculation
{
    decimal? a { get; set; }
    decimal? b { get; set; }
}

class myDto
{
    public List<CostPeriodDto> costPeriodList{ get; set; }

    public List<IPeriodCalculation> periodCalcList
    {
        get
        {
            return this.costPeriodList;  // compile error   
        }
    }
}
class CostPeriodTo:IPeriodCalculation
{
公共十进制数?a{get;set;}
公共十进制数?b{get;set;}
公共十进制数?c{get;set;}
公共十进制数?d{get;set;}
}
界面周期计算
{
十进制?a{get;set;}
十进制?b{get;set;}
}
类myDto
{
公共列表costPeriodList{get;set;}
公共列表周期
{
得到
{
返回this.costPeriodList;//编译错误
}
}
}

执行此操作的最佳方法是什么?

尝试
返回此.costPeriodList.Cast().ToList()
使用
Cast()

公共类成本周期到:i周期计算
{
公共十进制数?a{get;set;}
公共十进制数?b{get;set;}
公共十进制数?c{get;set;}
公共十进制数?d{get;set;}
}
公共接口i周期计算
{
十进制?a{get;set;}
十进制?b{get;set;}
}
公共类myDto
{
公共列表costPeriodList{get;set;}
公共列表周期
{
得到
{
返回此.costPeriodList.Cast().ToList();
}
}
}
我相信在C#4中,如果您使用的是实现IEnumerable的东西,您可以简单地按照编写它的方式来做,并且可以使用

类myDto
{ 
公共IEnumerable costPeriodList{get;set;}
公共可数周期
{ 
得到
{ 
返回this.costPeriodList;//不会给出编译错误
} 
} 
} 

从一个序列转换到另一个序列的LINQ方法将不相等。也就是说,如果使用
Cast()/ToList()
,以下测试将失败

此外,使用这些方法意味着,如果您试图将项目添加到一个集合中,它们将不会反映在另一个集合中。每次你调用periodCalcList,它都会创建一个全新的集合,这可能是灾难性的,取决于有多少项,调用的频率,等等

在我看来,一个更好的解决方案是不使用
List
来保存成本周期,而是使用从
collection
派生的集合,并显式实现
IEnumerable
。如果需要,您可以选择实现IList

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; }
    }
}
分类成本周期数据收集:
收集
数不清
{
IEnumerable.GetEnumerator(){
foreach(本表中的i期计算项目){
收益回报项目;
}
}
}
类MyDto{
公共成本周期收集成本周期{get;set;}
公共可数周期{
获取{return CostPeriods;}
}
}
class myDto 
{ 
    public IEnumerable<CostPeriodDto> costPeriodList{ get; set; } 

    public IEnumerable<IPeriodCalculation> periodCalcList 
    { 
        get 
        { 
            return this.costPeriodList;  // wont give a compilation error    
        } 
    } 
} 
Assert.AreSame(myDto.costPeriodList, myDto.periodCalcList);
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; }
    }
}