C# 获取特定列的实体框架Lambda表达式
我有以下查询,但它抛出错误: 无法将system.collections.generics.List隐式转换为system.collections.generics.ienumerable 查询:C# 获取特定列的实体框架Lambda表达式,c#,.net,asp.net-mvc,entity-framework,linq-to-entities,C#,.net,Asp.net Mvc,Entity Framework,Linq To Entities,我有以下查询,但它抛出错误: 无法将system.collections.generics.List隐式转换为system.collections.generics.ienumerable 查询: public IEnumerable<ApplicationUser> GetUsersByRole(string roleName) { var role = _context.Roles.FirstOrDefault(r => r.Name == roleName);
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();
}
我也有错误
无法将属性索引器ApplicationUser.Fullname分配给--它是只读的
有没有办法在不添加setter的情况下使fullname属性保持只读?您正在投影到一个匿名类型。你必须使用
.Select(u => new ApplicationUser { Id = u.Id, Name = u.UserName}).ToList();
要返回一个
IEnumerable
,请选择(u=>newapplicationuser{…}).ToList()
我现在收到以下错误:属性索引器ApplicationUser.Fullname无法分配给--它是只读的
请参见上面的编辑?
.Select(u => new ApplicationUser { Id = u.Id, Name = u.UserName}).ToList();