Saturday, 28 September 2013

Two classes inside a superclass for use with Entity Framework

Two classes inside a superclass for use with Entity Framework

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;
}
}

No comments:

Post a Comment