簡體   English   中英

實體框架Lambda表達式獲取特定列

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM