I’m a fairly junior C# developer so excuse me if this is trivial, but I am coming up with this error “invalid initializer member declarator” at the line of code I indicate below.
Can someone explain what this means and how do I work around/achieve this result? All of my data annotations are already applied once in PODetail, which is why I didn’t want to repeat the properties here as well.
public class PODetailsListViewModel : IViewModelList<PODetail, PODetailsListViewModel>, IEntity
{
public int Id { get; set; }
public string EntityDescription
{
get
{ return this.Id.ToString(); }
}
public PODetail PODetail { get; set; }
public IEnumerable<PODetailsListViewModel> ConvertClassToViewModel(IEnumerable<PODetail> poDetails)
{
IEnumerable<PODetailsListViewModel> contactGrid =
from l in poDetails.ToList()
select new PODetailsListViewModel()
{
Id = l.Id,
PODetail.POHeaderId = l.POHeaderId, <===== ERROR on this Line
....
};
return contactGrid;
}
You can’t access complex object properties in a class initializer like that (as you are attempting with the
PODetail.POHeaderIdproperty). You might have to implement a small workaround: create a collection of anonymous objects with the information you have already, then reconstruct a concrete list ofPODetailsListViewModels right afterwards:And then you can manually recreate what you’re looking for:
EDIT: Or perhaps you could nest the initializers: