简体   繁体   中英

Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

I have a method that aims to fill a PersonModel from OleDB;

public IEnumerable<PeopleModel> GetPeopleDetails()
{
    var constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
    using (var dbfCon = new OleDbConnection(constr))
    {
        dbfCon.Open();
        using (var dbfCmd = dbfCon.CreateCommand())
        {
            dbfCmd.CommandText = "SELECT pp_firstname, pp_surname, pp_title, pp_compnm, pp_hmaddr1, pp_hmaddr2, pp_hmtown, pp_hmcounty, pp_hmpcode, pp_spouse, pp_children FROM people ORDERBY pp_surname";
            using (var myReader = dbfCmd.ExecuteReader())
            {
                var peopleList = new List<PeopleModel>();
                while (myReader.Read())
                {
                    var details = new PeopleModel
                    {
                        details.Firstname = myReader[0].ToString(),
                        details.Lastname = myReader[1].ToString(),
                        details.Title = myReader[2].ToString(),
                        details.Company = myReader[3].ToString(),
                        details.Addr1 = myReader[4].ToString(),
                        details.Addr2 = myReader[5].ToString(),
                        details.Town = myReader[6].ToString(),
                        details.County = myReader[7].ToString(),
                        details.Spouse = myReader[8].ToString(),
                        details.Children = myReader[9].ToString(),                            
                    };
                    peopleList.Add(details);
                }
                return peopleList;
            }
        }
    }
} 

This code is pretty much identical to the method I am using to fill a companies details, which works no problem. Here is the PeopleModel I am using to build a person.

namespace SdcDatabase.Model
{
    public class PeopleModel
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Title { get; set; }
        public string Company { get; set; }
        public string Addr1 { get; set; }
        public string Addr2 { get; set; }
        public string Town { get; set; }
        public string County { get; set; }
        public string Spouse { get; set; }
        public string Children { get; set; }
    }
}

Although the companies method has worked fine previously, I am now getting the following error when I try to build my project after implementing the People code: Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

I really am at a lost cause with this as it is working in an almost identical method for a Company.

Correct syntax, without details. in the assignments in the initializer:

var details = new PeopleModel
{
    Firstname = myReader[0].ToString(),
    Lastname = myReader[1].ToString(),
    Title = myReader[2].ToString(),
    Company = myReader[3].ToString(),
    Addr1 = myReader[4].ToString(),
    Addr2 = myReader[5].ToString(),
    Town = myReader[6].ToString(),
    County = myReader[7].ToString(),
    Spouse = myReader[8].ToString(),
    Children = myReader[9].ToString(),                            
};

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