1

我可以在我的应用程序中毫无问题地执行以下代码片段。

Projects.Select(s =>
                        new ProjectName()
                        {
                            ProjectID = s.ProjectID,
                            Name = s.Name
                        })  

请注意,ProjectName 是一个无参数构造函数,存在于以下类中:

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

但是,我无法在我的应用程序中运行以下查询。没有设计时编译错误。我只在运行时在“Catch”语句中收到这篇文章主题的错误。

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
                            }

                        })

请注意,CustomerProjectDDL 是一个无参数构造函数,它包含两个类。

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

我的视图中需要这两个类,因为它需要两者的属性。Customer 对象是使用 DbContext 从 Visual Studio 中 ORM 上的代码生成项生成的,DbContext 具有其所有属性,并且 ProjectName 类是我在上面的帖子中指定的第一个类。

我将不胜感激任何帮助...

除了使用AutoMapper在新的 ViewModel 中手动输入属性之外,您如何从一个模型中的两个或多个单独的表中获取数据以满足视图?

我认为我所做的是一个很好的概念。获取任何生成的 DbContext 或任何其他类,只需将它们放在超类中(如 CustomerProjectDDL 类)。

4

1 回答 1

3

在对象上添加new关键字.Select

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

贤者天空36

我的客户对象如下所示:

//------------------------------------------------------------------------------
// <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; }
    }
}
于 2013-10-17T19:23:19.080 回答