說我有2個屬性如何使用lambda將類列表對象添加到類中?
class TestClass
{
public int propertyOne {get;set;}
public List<int> propertyTwo {get; private set;}
public TestClass()
{
propertyTwo = new List<int>();
}
}
使用LINQ,我想如下創建的TestClass列表的類:
var results = from x in MyOtherClass
select new TestClass()
{
propertyOne = x.propertyFirst,
propertyTwo = x.propertyList
};
propertyTwo = x.propertyList居然拋出一個錯誤,帶着紅色的下劃線。
如何在這種情況下實現propertyTwo.AddRange(other)的等價物?
乾杯
是propertyTwo應該有一個'私人'二傳手。 – 2009-12-21 23:09:23
是的,並在班級建設期間初始化。 在正常情況下,它會是這樣的: TestClass newClass = new TestClass(); newClass.propertyTwo.AddRange(new List(){1,2,3,4}); –
Joshscorp
2009-12-21 23:12:25