我正在尝试使用包含嵌套类数组的 LINQ 填充实例。我已经设法使用以下 LINQ 做到这一点。我还在选择中包含了构成实例的类。
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; }
}
这很有效,我可以使用WarrantYear[0]
或访问值WarrantYear1
。这在设计 UI 时有时很有用。但是,在这种情况下,我不需要该WarrantYear1
属性,因为我将其转换为 JSON,并且不需要重复(或者想在网络上发送相同数据的两个版本)。我的问题是,如何编写选择语句来加载 WarrantYear 数组。或者我如何编写类,以便我可以将数组作为属性访问。我的解决方案不应包含类中的Warrant1, Warrant2, Warrant3
属性EditableWarrantBook
。