1

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).

sagesky36
  • 4,542
  • 19
  • 82
  • 130

1 Answers1

3

Add new keyword when you .Select on the object:

Projects.Where(w => w.Status.Description == "Not Started").Select(s =>
                        new CustomerProjectDDL()
                        {
                            ProjectName = new ProjectName()
                            {
                                ProjectID = s.Project,
                                Name = s.Name
                            },

                            Customer = new Customer()
                            {
                                CustomerID = s.CustomerID,
                                Email = s.Customer.Email,
                                City = s.Customer.City,
                                State = s.Customer.State,
                                Zip = s.Customer.Zip
                            }    
                        });

sagesky36

My Customer object looks like this:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace YeagerTechModel
{
    using System;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Collections.Generic;

    [Serializable,  DataContract(IsReference = true)] 
     public partial class Customer
    {
        public Customer()
        {
            this.Projects = new HashSet<Project>();
        }

        [DataMember] public short CustomerID { get; set; }
        [DataMember] public string UserName { get; set; }
        [DataMember] public string Email { get; set; }
        [DataMember] public string Company { get; set; }
        [DataMember] public string FirstName { get; set; }
        [DataMember] public string LastName { get; set; }
        [DataMember] public string Address1 { get; set; }
        [DataMember] public string Address2 { get; set; }
        [DataMember] public string City { get; set; }
        [DataMember] public string State { get; set; }
        [DataMember] public string Zip { get; set; }
        [DataMember] public string HomePhone { get; set; }
        [DataMember] public string CellPhone { get; set; }
        [DataMember] public string Website { get; set; }
        [DataMember] public string IMAddress { get; set; }
        [DataMember] public System.DateTime CreatedDate { get; set; }
        [DataMember] public Nullable<System.DateTime> UpdatedDate { get; set; }

        [DataMember] public virtual ICollection<Project> Projects { get; set; }
    }
}
sagesky36
  • 4,542
  • 19
  • 82
  • 130
gleng
  • 6,185
  • 4
  • 21
  • 35
  • Easy to make stupid mistakes when you rush trying to answer, @Habib ;) – gleng Oct 17 '13 at 19:27
  • Almost there.... I am now getting the following error: "{"The entity or complex type 'YeagerTechModel.Customer' cannot be constructed in a LINQ to Entities query."}" I will edit my post and let you see what is inside the Customer class. Please let me know how I can modify it to satisfy the linq query. – sagesky36 Oct 17 '13 at 19:43
  • @sagesky36 look at this -> http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query – gleng Oct 17 '13 at 19:48
  • 1
    I see.... Instead of actually using the mapped entity class, I created another class in my ViewModel folder and copied the code from the generated class into the ViewModel class. I'll update my code to show you what I did. Thanks so much for your help. – sagesky36 Oct 17 '13 at 20:28