private static Expression <Func <POLLUTANTRELEASE, bool> > getLambdaExpression(PollutantReleasesTimeSeriesFilter filter) { ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "p"); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filter, param); Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); return(lambda); }
/// <summary> /// getPollutantReleasesLambda /// </summary> private static Expression <Func <POLLUTANTRELEASE, bool> > getPollutantReleasesLambda(DataClassesPollutantReleaseDataContext db, IndustrialActivitySearchFilter filter) { ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "s"); PollutantReleaseSearchFilter filterReleases = FilterConverter.ConvertToPollutantReleaseSearchFilter(filter); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filterReleases, param); Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); return(lambda); }
private static Expression <Func <POLLUTANTRELEASE, bool> > getLambdaExpression(PollutantReleasesTimeSeriesFilter filter, MediumFilter.Medium medium) { // apply filter ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "p"); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filter, param); //apply medium condition Expression expMedium = LinqExpressionBuilder.GetLinqExpressionMediumRelease(medium, param); exp = LinqExpressionBuilder.CombineAnd(exp, expMedium); Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); return(lambda); }
// --------------------------------------------------------------------------------------------------- // Map filter // --------------------------------------------------------------------------------------------------- #region Map /// <summary> /// returns the MapFilter (sql and layers) corresponding to the filter. /// </summary> public static MapFilter GetMapFilter(PollutantReleasesTimeSeriesFilter filter) { //parameter must be "p" to match map config file ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "p"); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filter, param); Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); // create sql and sectors to map. Do not remove parameter prefix. MapFilter mapFilter = new MapFilter(); mapFilter.SqlWhere = LinqExpressionBuilder.GetSQL(lambda.Body, null); mapFilter.SetLayers(filter.ActivityFilter); return(mapFilter); }
/// <summary> /// return total list of areas /// </summary> public static IEnumerable <IAReleasesTreeListRow> GetPollutantReleases(IndustrialActivitySearchFilter filter) { DataClassesPollutantReleaseDataContext db = getPollutantReleaseDataContext(); ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "s"); PollutantReleaseSearchFilter filterRelease = FilterConverter.ConvertToPollutantReleaseSearchFilter(filter); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filterRelease, param); Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); int facilitiesCount = 0; List <IAReleasesTreeListRow> pollutants = getReleases(db, lambda, out facilitiesCount).ToList <IAReleasesTreeListRow>(); filter.Count = facilitiesCount; return(pollutants); }
// --------------------------------------------------------------------------------------------------- // Facility couting // --------------------------------------------------------------------------------------------------- #region Facility counting /// <summary> /// Returns the number of facilities for each medium type. /// </summary> public static PollutantReleases.FacilityCountObject GetFacilityCounts(PollutantReleasesTimeSeriesFilter tsFilter) { // removes all year span information var filter = FilterConverter.ConvertToPollutantReleaseSearchFilter(tsFilter); ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "POLLUTANTRELEASE"); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filter, param); // Exclude EPER reporting years Expression prop = Expression.Property(param, "ReportingYear"); Expression val = Expression.Constant(2007); Expression yearExp = Expression.GreaterThanOrEqual(prop, val); exp = Expression.AndAlso(yearExp, exp); Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); return(PollutantReleases.GetFacilityCounts(lambda, filter.MediumFilter)); }
// --------------------------------------------------------------------------------------------------- // Comparison // --------------------------------------------------------------------------------------------------- #region comparison /// <summary> /// GetComparisonTimeSeries /// </summary> public static TimeSeriesClasses.ComparisonPollutant GetComparisonTimeSeries(PollutantReleasesTimeSeriesFilter filter, int yearFrom, int yearTo, MediumFilter.Medium medium) { // Create lambda with pollutant release filter DataClassesPollutantReleaseDataContext db = getDataContext(); ParameterExpression param = Expression.Parameter(typeof(POLLUTANTRELEASE), "s"); Expression exp = LinqExpressionBuilder.GetLinqExpressionPollutantReleases(filter, param); // apply medium Expression expMedium = LinqExpressionBuilder.GetLinqExpressionMediumRelease(medium, param); if (exp != null && expMedium != null) { exp = Expression.And(exp, expMedium); } Expression <Func <POLLUTANTRELEASE, bool> > lambda = Expression.Lambda <Func <POLLUTANTRELEASE, bool> >(exp, param); // group by reporting year, get from and to data IQueryable <IGrouping <int, POLLUTANTRELEASE> > groupFrom = db.POLLUTANTRELEASEs.Where(lambda).Where(p => p.ReportingYear == yearFrom).GroupBy(p => p.ReportingYear); IQueryable <IGrouping <int, POLLUTANTRELEASE> > groupTo = db.POLLUTANTRELEASEs.Where(lambda).Where(p => p.ReportingYear == yearTo).GroupBy(p => p.ReportingYear); // Facility IDs when year is 'yearTo' var vTo = db.POLLUTANTRELEASEs.Where(lambda).Where(p => p.ReportingYear == yearTo).Select(p => p.FacilityID).Distinct(); IQueryable <IGrouping <int, POLLUTANTRELEASE> > groupDataFromBoth = db.POLLUTANTRELEASEs.Where(lambda).Where(p => p.ReportingYear == yearFrom && vTo.Contains(p.FacilityID)).GroupBy(p => p.ReportingYear); // Facility IDs when year is 'yearFrom' var vFrom = db.POLLUTANTRELEASEs.Where(lambda).Where(p => p.ReportingYear == yearFrom).Select(p => p.FacilityID).Distinct(); IQueryable <IGrouping <int, POLLUTANTRELEASE> > groupDataToBoth = db.POLLUTANTRELEASEs.Where(lambda).Where(p => p.ReportingYear == yearTo && vFrom.Contains(p.FacilityID)).GroupBy(p => p.ReportingYear); // result lists IEnumerable <TimeSeriesClasses.TsPollutantCompare> dataFrom = null, dataTo = null; IEnumerable <TimeSeriesClasses.TsPollutantCompare> dataFromBoth = null, dataToBoth = null; switch (medium) { case MediumFilter.Medium.Air: dataFrom = groupFrom.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityAir), x.Sum(p => p.QuantityAccidentalAir))); dataTo = groupTo.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityAir), x.Sum(p => p.QuantityAccidentalAir))); dataFromBoth = groupDataFromBoth.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityAir), x.Sum(p => p.QuantityAccidentalAir))); dataToBoth = groupDataToBoth.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityAir), x.Sum(p => p.QuantityAccidentalAir))); break; case MediumFilter.Medium.Water: dataFrom = groupFrom.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityWater), x.Sum(p => p.QuantityAccidentalWater))); dataTo = groupTo.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityWater), x.Sum(p => p.QuantityAccidentalWater))); dataFromBoth = groupDataFromBoth.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityWater), x.Sum(p => p.QuantityAccidentalWater))); dataToBoth = groupDataToBoth.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantityWater), x.Sum(p => p.QuantityAccidentalWater))); break; case MediumFilter.Medium.Soil: dataFrom = groupFrom.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantitySoil), x.Sum(p => p.QuantityAccidentalSoil))); dataTo = groupTo.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantitySoil), x.Sum(p => p.QuantityAccidentalSoil))); dataFromBoth = groupDataFromBoth.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantitySoil), x.Sum(p => p.QuantityAccidentalSoil))); dataToBoth = groupDataToBoth.Select(x => new TimeSeriesClasses.TsPollutantCompare(x.Count(), x.Sum(p => p.QuantitySoil), x.Sum(p => p.QuantityAccidentalSoil))); break; default: return(null); } TimeSeriesClasses.ComparisonPollutant result = new TimeSeriesClasses.ComparisonPollutant(yearFrom, yearTo); var res = dataFrom.SingleOrDefault(); if (res != null) { result.SetFrom(res.Count, res.Quantity, res.Accidental); } res = dataTo.SingleOrDefault(); if (res != null) { result.SetTo(res.Count, res.Quantity, res.Accidental); } res = dataFromBoth.SingleOrDefault(); if (res != null) { result.SetBothFrom(res.Count, res.Quantity, res.Accidental); } res = dataToBoth.SingleOrDefault(); if (res != null) { result.SetBothTo(res.Count, res.Quantity, res.Accidental); } return(result); }