简体   繁体   中英

object initialization, why collection initializer is being called

First off, I know that dropdownlists should each be separate in their own ViewModel, preferably emitted in a partial view.

However, if you could help me with the below problem, this will inform me why I'm having trouble getting this to compile at design time with the two classes inside my superclass (since I need them in the same View.

If I have a superclass described as below:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using YeagerTechModel.DropDownLists;

namespace YeagerTechModel.ViewModels
{
    [DataContract]
    [Serializable]
    public partial class CustomerProjectDDL
    {
        [DataMember]
        public Customer Customer = new Customer();
        [DataMember]
        public ProjectName ProjectName = new ProjectName();
    }
}

and the definitions of the ProjectName class is as follows:

namespace YeagerTechModel.DropDownLists

    {
        [DataContract]
        [Serializable]
        public partial class ProjectName
        {
            [DataMember]
            public Int16 ProjectID { get; set; }
            [DataMember]
            public String Name { get; set; }
        }
    }

I'm getting the design time compile error below in my method when I try and use the above: The left curly brace right after the new statement: "Cannot initialize type 'YeagertechModel.ViewModels.CustomerProjectDDL' with a collection initializer because it does not implement IENumerable."

public List<CustomerProjectDDL> GetProjectNameDropDownListVM()
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    var project = DbContext.Projects.Where(w => w.ProjectID > 0).Select(s =>
                        new CustomerProjectDDL()
                        {
                            ProjectName.ProjectID = s.ProjectID,
                            ProjectName.Name = s.Name
                        });


                    List<CustomerProjectDDL> myProjects = new List<CustomerProjectDDL>();

                    myProjects = project.ToList();

                    return myProjects;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

It will work if you can use syntax like this

    new CustomerProjectDDL()
    {
        ProjectName = {ProjectID = s.ProjectID, Name = s.Name}
    });

This has to do with object initialization, specifically with the order of initialization. However, I am too unable to get why this happens because the object initializer calls the default constructor first, only after then the values are assigned. And to my knowledge, during object initialization, instance fiels are initialized before constructor is called, so the ProjectName property should have been initialized by the time you tried to access it in the object initializer

Can you change

  var project = DbContext.Projects.Where(w => w.ProjectID > 0).Select(s =>
                    new CustomerProjectDDL()
                    {
                        ProjectName.ProjectID = s.ProjectID,
                        ProjectName.Name = s.Name
                    });

To

  var project = DbContext.Projects.Where(w => w.ProjectID > 0).Select(s =>
                    new CustomerProjectDDL
                    {
                        ProjectName = s
                    });

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM