2017-01-10 76 views
0

這是ApplicationUser:過濾DbSet中的EntityFramework CodeFirst

public class ApplicationUser : IdentityUser<long> 
{ 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 
    public UserTypes Type { get; set; } 
    public string FullName { get { return $"{Firstname ?? ""} {Lastname ?? ""}".Trim(); } } 
} 

我們有3個不同的UserTypes(供應商,支持者,NormalUser (這是ApplicationUser)

public class Provider : ApplicationUser{ 
    // Provider related virtual Icollections 
} 

public class Supporter : ApplicationUser{ 
    // Supporter related virtual Icollections 
} 

現在ApplicationDbContext我想在應用程序旁邊有這些DbSet s:

public virtual DbSet<Provider> Providers{get;set;} 
public virtual DbSet<Supporter> Supporters{get;set;} 

哪個DbSet<Provider>應該返回ApplicationUsers他們UserTypes等於2(例如)

+0

什麼UserTypes的屬性? –

+0

@ H.Herzl以及它是一個枚舉'{Normal = 1,Provider = 2,Supporter = 3}' –

+0

你試過這個嗎? var query = dbContext.Providers.Where(item => item.Type == UserTypes.Provider).ToList(); –

回答

1
 public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<long>, long> 
     { 

      public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
       : base(options) 
      { 
      } 
      public ApplicationDbContext() 
       : base() 
      { 
      } 

      public virtual IEnumerable<Provider> Providers 
      { 
       get 
       { 
        return (IEnumerable<Provider>)Users.Where(z => z.Type == UserTypes.Provider).AsEnumerable(); 
       } 
      } 
     } 
相關問題