![](/img/trans.png)
[英]How to filter entity framework result with multiple columns using a lambda expression
[英]Entity Framework Lambda Expression To Get Specific Columns
我有以下查詢,但拋出錯誤:
無法將system.collections.generics.lists <>隱式轉換為system.collections.generics.ienumerable
查詢:
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();
}
在我的應用程序用戶類中,我具有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); }
}
}
我也得到錯誤
屬性Indexer ApplicationUser.Fullname不能分配給它-只讀
有什么方法可以在不添加setter的情況下使fullname屬性保持只讀?
您正在投影成匿名類型。 您將不得不使用
.Select(u => new ApplicationUser { Id = u.Id, Name = u.UserName}).ToList();
返回IEnumerable<ApplicationUser>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.