mardi 28 juin 2016

linq - multiple where statements if variables are not null

I would like the query to return from WANOM_DETAILS & STRDT tables where PIPELINE = 'F' AND HISTORICAL_FLAG <> 'T' AND INSTALLATION = 'EA' AND ANOMALY_CLASS = 'Critical'.

The if statements work correctly, but at the moment the query is only returning from WANOM_DETAILS & STRDT tables where ANOMALY_CLASS = 'Critical' as this is the final line before the model.ToList();

Any help would be much appreciated. Here is the code;

        strdt = "EA";
        anom_class = "Critical";

        var models = db.WANOM_DETAILS;

        var model = models.Where(wanom_details => wanom_details.STRDT.PIPELINE == "F");
        model = models.Where(wanom_details => wanom_details.STRDT.HISTORICAL_FLAG != "T");
        if (strdt != "")
        {
            model = models.Where(wanom_details => wanom_details.INSTALLATION == strdt);
        }
        if (anom_class != "")
        {
            model = models.Where(wanom_details => wanom_details.ANOMALY_CLASS == anom_class);
        }
        if (subdt != "")
        {
            model = models.Where(wanom_details => wanom_details.WANOM.SUBSTRUCTURE == subdt);
        }
        if (comp != "")
        {
            model = models.Where(wanom_details => wanom_details.WANOM.TYPE + "-" + wanom_details.WANOM.IDENT == comp);
        }

        return PartialView("_SiteAnomalyGridViewPartial", model.ToList());

Aucun commentaire:

Enregistrer un commentaire