简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM