I am able to execute the below code snippet in my application with no problem.
Projects.Select(s =>
new ProjectName()
{
ProjectID = s.ProjectID,
Name = s.Name
})
Note that ProjectName is a parameterless constructor which exists in the following class:
namespace YeagerTechModel.DropDownLists
{
[DataContract]
[Serializable]
public partial class ProjectName
{
[DataMember]
public Int16 ProjectID { get; set; }
[DataMember]
public String Name { get; set; }
}
}
However, I am not able to run the following query in my application. There are no design time compile errors. I only get the error in the subject of this post at run time in a "Catch" statement.
Projects.Where(w => w.Status.Description == "Not Started").Select(s =>
new CustomerProjectDDL()
{
ProjectName =
{
ProjectID = s.Project,
Name = s.Name
},
Customer =
{
CustomerID = s.CustomerID,
Email = s.Customer.Email,
City = s.Customer.City,
State = s.Customer.State,
Zip = s.Customer.Zip
}
})
Note that CustomerProjectDDL is a parameterless constructor which contains two classes.
namespace YeagerTechModel.ViewModels
{
[DataContract]
[Serializable]
public partial class CustomerProjectDDL
{
[DataMember]
public Customer Customer = new Customer();
[DataMember]
public ProjectName ProjectName = new ProjectName();
}
}
I need both of these classes in my View since it needs properties from both. The Customer object was generated from a Code generation item on the ORM in Visual Studio using DbContext which has all its properties and the ProjectName class is the first class I specified above in the post.
I would appreciate any help...
Besides using AutoMapper and manually typing in the properties into your new ViewModel, how do you get data from two or more separate tables in one model to satisfy the View?
I thought what I did was a good concept. Take any of the generated DbContext or any other classes and simply place them inside a superclass (like the CustomerProjectDDL class).