Search code examples
c#linqentity-frameworkef-code-first

The specified type member 'Title' is not supported in LINQ to Entities


I got an error when using Title property in my Linq to Entity:

The specified type member 'Title' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

my query is:

        var db = FaraWorkspaceEntity.GetEntity();

        var query = from item in db.ProjectManagers
                    where item.ProjectID == projectID
                    select new UserListItem
                    {
                        ID = item.UserID,
                        Title = item.User.Title // Here is error
                    };

        return query.ToList();



    public class User : IdentityUser<string, UserLogin, UserRole, UserClaim>
    {
        [Required]
        [Display(Name = "نام")]
        [StringLength(50)]
        public string Firstname { get; set; }

        [Required]
        [Display(Name = "نام خانوادگی")]
        [StringLength(50)]
        public string Lastname { get; set; }

        public string Title
        {
            get { return this.Firstname + " " + this.Lastname; }
        }
    }

Solution

  • Title is a property in your entity. In your table there is not such column. Entity Framework can not convert your code to Sql. So, you must change your code as:

      var query = from item in db.ProjectManagers
                  where item.ProjectID == projectID
                  select new UserListItem
                  {
                       ID = item.UserID,
                       Title = item.User.Firstname + " " + item.User.Lastname;
                  };
    

    But, I suggest you to select FirstName and LastName, and when you want to get the value of Title your getter accessor will work:

      var query = from item in db.ProjectManagers
                  where item.ProjectID == projectID
                  select new UserListItem
                  {
                       ID = item.UserID,
                       Firstname = item.User.Firstname
                       Lastname =  item.User.Lastname;
                  };