Say I have a class with 2 properties
class TestClass
{
public int propertyOne {get;set;}
public List<int> propertyTwo {get; private set;}
public TestClass()
{
propertyTwo = new List<int>();
}
}
Using linq, I am trying to create a list of TestClass as follows:
var results = from x in MyOtherClass
select new TestClass()
{
propertyOne = x.propertyFirst,
propertyTwo = x.propertyList
};
propertyTwo = x.propertyList actually throws an error, with the squiggly red underline.
How can I implement the equivalent of the propertyTwo.AddRange(other) in this case?
Cheers
As others have said you can’t set the
propertyTwothat way since its declared private. If you just want to set it on construction you could add a second constructor that allows you to pass an initial list, giving you: