简体   繁体   中英

C# Filter on property of derived Class

I have class called Dossier which is my Base Class and a classes called PensioenDossier and AovDossier which inherits Dossier.

public partial class Dossier
{
    public int ID { get; set; }
    public int ProductID { get; set; }
    public string Nummer { get; set; }
}
public partial class PensioenDossier : Dossier
{
    public decimal Premie { get; set; }
    public decimal? PartnerPensioen { get; set; }
}
public partial class AovDossier : Dossier
{
    public decimal VerzekerdKapitaal { get; set; }
    public decimal? MaandPremie { get; set; }
}

Now I have a process where I select a Product(Pensioen/AOV) and want to filter based on the selected Product. So I get the all Dossiers by

Context.Dossiers

I then filter on ProductID. And if its Pensioen I would like to filter on for example "Premie" and if it is AOV then I want to filter on "MaandPremie". But since I got Context.Dossiers I am wondering if it possible to filter on the derived class properties?

Depending on how time critical things are, here is one way;

var allDossiers = Context.Dossiers.AsQueryable();
var filteredPensioenDossiers = allDossiers.OfType<PensioenDossier>().Where(d => d.Premie > 1000);
var filteredAovDossiers = allDossiers.OfType<AovDossier>().Where(d => d.MaandPremie.HasValue);

var allFilteredDossiers = (filteredPensioenDossers as Dossier).Concat(filteredAovDossiers as Dossier);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM