I am trying to populate an instance with LINQ containing an array of nested classes. I have managed to do this with the following LINQ. I have also included the classes that make up the instance in the select.
select new EditableWarrantBook {
Id = p1.id,
Comment = p1.EntryComment,
WarrantYear1 = new BookYear {
StatusYear = p2.StatusYear,
Status = p2.Status,
},
WarrantYear2 = new BookYear {
StatusYear = p3.StatusYear,
Status = p3.Status,
},
WarrantYear3 = new BookYear {
StatusYear = p4.StatusYear,
Status = p4.Status,
}
}
public class EditableWarrantBook
{
public int Id { get; set; }
public string Comment { get; set; }
public BookYear[] WarrantYear = new BookYear[3];
public BookYear WarrantYear1
{
get { return WarrantYear[0]; }
set { WarrantYear[0] = value; }
}
public BookYear WarrantYear2
{
get { return WarrantYear[1]; }
set { WarrantYear[1] = value; }
}
public BookYear WarrantYear3
{
get { return WarrantYear[2]; }
set { WarrantYear[2] = value; }
}
}
public class BookYear
{
public int? StatusYear { get; set; }
public string Status { get; set; }
}
This works I can access values with either WarrantYear[0]
or WarrantYear1
. This can sometimes be useful when designing UI. However, in this case, I don't need the WarrantYear1
property because I am turning this into JSON and I don't need to repeat (or want to send two versions of the same data on the network). My question is, how do I write the select statement to load WarrantYear array. Or how do I write the class so that I can access the array as a property. My solution should not contain the Warrant1, Warrant2, Warrant3
properties in the EditableWarrantBook
Class.