[英]Entity Framework Lambda Expression To Get Specific Columns
I have the following query but it's throwing error: 我有以下查询,但抛出错误:
Cannot implicitly convert system.collections.generics.lists <> to system.collections.generics.ienumerable
无法将system.collections.generics.lists <>隐式转换为system.collections.generics.ienumerable
The query: 查询:
public IEnumerable<ApplicationUser> GetUsersByRole(string roleName)
{
var role = _context.Roles.FirstOrDefault(r => r.Name == roleName);
return _context.Users
.Where(u => u.Roles.Any(r => r.RoleId == role.Id))
.Select(u => new ApplicationUser { Id = u.Id, FullName = u.FullName })
.ToList();
}
In my application user class I have the Fullname
property defined as follows: 在我的应用程序用户类中,我具有
Fullname
属性,定义如下:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
}
I also get error 我也得到错误
property Indexer ApplicationUser.Fullname cannot be assigned to -- it is read only
属性Indexer ApplicationUser.Fullname不能分配给它-只读
Is there any way to keep the fullname property read only without adding a setter? 有什么方法可以在不添加setter的情况下使fullname属性保持只读?
You're projecting into an anonymous type. 您正在投影成匿名类型。 You would have to use
您将不得不使用
.Select(u => new ApplicationUser { Id = u.Id, Name = u.UserName}).ToList();
To return an IEnumerable<ApplicationUser>
返回
IEnumerable<ApplicationUser>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.