Moh*_*deh 3 c# linq entity-framework ef-code-first
我Title在Linq to Entity中使用属性时遇到错误:
LINQ to Entities不支持指定的类型成员'Title'.仅支持初始值设定项,实体成员和实体导航属性.
我的查询是:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
Title是您实体的财产.在你的表中没有这样的列.Entity Framework无法将您的代码转换为Sql.因此,您必须将代码更改为:
var query = from item in db.ProjectManagers
where item.ProjectID == projectID
select new UserListItem
{
ID = item.UserID,
Title = item.User.Firstname + " " + item.User.Lastname;
};
Run Code Online (Sandbox Code Playgroud)
但是,我建议你选择FirstName和LastName,当你想获得Title你的getter访问器的价值时会起作用:
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;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1432 次 |
| 最近记录: |