I have a problems with adding subelements off AddressElement objects
My test class:
public class AddressElement
{
public AddressElement[] AddressElement1 { get; set; }
public string PostIndex { get; set; }
}
My test interface:
public interface IAddress
{
string PostIndex { get; }
}
private AddressElement CreateAddressElement(IAddress address)
{
return new AddressElement
{
PostIndex = address.PostIndex
};
}
private int startIndex = 0;
private int finalIndex = 5;
Get elements method:
private AddressElement[] GetAddressElements(List addresses, int startIndex, int finalIndex) { List addressElements = new List();
for (int i = startIndex; i < finalIndex; i++)
{
if (i == startIndex)
{
addressElements.Add(CreateAddressElement(addresses[i]));
if (i != startIndex )
{
AddressElement addressElement = addressElements[startIndex];
addressElements[startIndex].AddressElement1 = new[] { CreateAddressElement(addresses[i]) };
}
I need add all subelements of AddressElement in for cycle: addressElements[startIndex].AddressElement1[startIndex].AddressElement1[startIndex].AddressElement1[startIndex].AddressElement1[startIndex]....
You can do this by using Linq :
private AddressElement[] GetAddressElements(List addresses, int startIndex, int finalIndex) {
return addresses.Skip(startIndex).Take(finalIndex - startIndex).Select(s => CreateAddressElement(s)).ToArray() ;
}
Hope it will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.