LINQ查询中的转换错误

时间:2015-04-05 06:14:01

标签: c# linq entity-framework

我正在尝试执行一个在客户和项目实体之间具有层级关系的查询。

我正在使用子查询为Project实体带回我想要的字段,而不是使用" Include"这会带回所有领域。

由于以下错误,我无法编译它(请使用您最喜欢的查看器来放大图像)。 enter image description here

如果有人知道如何解决这个问题,我将不胜感激。

我通过执行生成有效结果集的匿名查询来完成下一个最好的事情。我现在想做的就是简单地转换"结果"变成我的"客户"将Project实体作为ICollection(客户的子级)的模型。

但是,我通过这种方法得到以下错误。 enter image description here

如果有人可以帮我解决这个问题,我也非常感激。

我知道每个错误的含义,但我们不太清楚如何进行转换。

编辑发布课程

客户类

namespace YeagerTechDB.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Runtime.Serialization;

    [Serializable, DataContract(IsReference = true)]

    public partial class Customer
    {
        public Customer()
        {
            Projects = new HashSet<Project>();
        }

        [Key]
        [ScaffoldColumn(true)]
        [Display(Name = "ID")]
        [DataMember]
        public short CustomerID { get; set; }

        [Required]
        [StringLength(256)]
        [DataMember]
        public string UserName { get; set; }

        [Required]
        [StringLength(50)]
        [EmailAddress]
        [DataMember]
        public string Email { get; set; }

        [StringLength(50)]
        [DataMember]
        public string Company { get; set; }

        [StringLength(50)]
        [DataMember]
        public string FirstName { get; set; }

        [StringLength(50)]
        [DataMember]
        public string LastName { get; set; }

        [StringLength(50)]
        [DataMember]
        public string Address1 { get; set; }

        [StringLength(50)]
        [DataMember]
        public string Address2 { get; set; }

        [StringLength(50)]
        [DataMember]
        public string City { get; set; }

        [StringLength(2)]
        [DataMember]
        public string State { get; set; }

        [StringLength(10)]
        [DataType(DataType.PostalCode)]
        [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Must match 99999 or 99999-9999 format")]
        [DataMember]
        public string Zip { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Must match 999-999-9999 format")]
        [DataMember]
        public string HomePhone { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Must match 999-999-9999 format")]
        [DataMember]
        public string CellPhone { get; set; }

        [StringLength(100)]
        [DataType(DataType.Url)]
        [DataMember]
        public string Website { get; set; }

        [StringLength(50)]
        [DataType(DataType.Url)]
        [DataMember]
        public string IMAddress { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Created")]
        [DataMember]
        public DateTime CreatedDate { get; set; }

        [Display(Name = "Updated")]
        [DataType(DataType.DateTime)]
        [DataMember]
        public DateTime? UpdatedDate { get; set; }

        [DataMember]
        public virtual ICollection<Project> Projects { get; set; }
    }
}

项目类

namespace YeagerTechDB.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Runtime.Serialization;

    [Serializable, DataContract(IsReference = true)]
    public partial class Project
    {
        [Key]
        [ScaffoldColumn(false)]
        [Editable(false)]
        [Display(Name = "Proj ID")]
        [DataMember]
        public short ProjectID { get; set; }

        [ScaffoldColumn(false)]
        [Editable(true)]
        [Display(Name = "Cust ID")]
        [DataMember]
        public short CustomerID { get; set; }

        [Required(ErrorMessage = "Required!")]
        [StringLength(30)]
        [Display(Name = "Project Name")]
        [DataMember]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        [DataMember]
        public string Description { get; set; }

        [ScaffoldColumn(true)]
        [Display(Name = "Category")]
        [DataMember]
        public short CategoryID { get; set; }

        [ScaffoldColumn(true)]
        [Display(Name = "Priority")]
        [DataMember]
        public short PriorityID { get; set; }

        [ScaffoldColumn(true)]
        [Display(Name = "Status")]
        [DataMember]
        public short StatusID { get; set; }

        [DataType(DataType.Currency)]
        [DataMember]
        public decimal? Quote { get; set; }

        [DataType(DataType.MultilineText)]
        [DataMember]
        public string Notes { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Created")]
        [DataMember]
        public DateTime CreatedDate { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Updated")]
        [DataMember]
        public DateTime? UpdatedDate { get; set; }

        [DataMember]
        public virtual Category Category { get; set; }
        [DataMember]
        public virtual Customer Customer { get; set; }
        [DataMember]
        public virtual Priority Priority { get; set; }
        [DataMember]
        public virtual Status Status { get; set; }
        [DataMember]
        public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
    }
}

0 个答案:

没有答案