I have following Linq queries where I retrieve data from two different tables with same filter and join; and finally union the result set.
var plannedReceiptsResult = db.OMS_Planned_Receipts.Where(p => p.Product == product && p.PeriodID >= StartPeriod && p.PeriodID <= EndPeriod)
.Join(db.Periods, c => c.PeriodID, o => o.PeriodID, (c, o) => new { c, o })
.Select(b => new PivotViewModel
{
Product = b.c.Product,
PeriodID = b.c.PeriodID,
SiteID = b.c.SiteID,
Value = b.c.Value,
Activity = "Planned Receipts",
PeriodStart = b.o.Period_Start,
PeriodEnd = b.o.Period_End,
PeriodDescription = b.o.Display
});
var systemForecastResult = db.OMS_System_Forecast.Where(p => p.Product == product && p.PeriodID >= StartPeriod && p.PeriodID <= EndPeriod)
.Join(db.Periods, c => c.PeriodID, o => o.PeriodID, (c, o) => new { c, o })
.Select(b => new PivotViewModel
{
Product = b.c.Product,
PeriodID = b.c.PeriodID,
SiteID = b.c.SiteID,
Value = b.c.Value,
Activity = "System Forecast",
PeriodStart = b.o.Period_Start,
PeriodEnd = b.o.Period_End,
PeriodDescription = b.o.Display
});
var activityResult = plannedReceiptsResult.Union(systemForecastResult);
I need to perform the same with another 8 tables and finally union the result set of them all. All my tables have the same schema. I'm hoping to simplify this with a delegate or method. Please advise.
Also please note that I'm using EF 5 with IDBSet Filtering as per applying global filters article
I think following method should be it but am not sure how I should be calling it.
public interface IEntity
{
Int16 TenantID { get; set; }
string Product { get; set; }
string SiteID { get; set; }
int PeriodID { get; set; }
double? Value { get; set; }
}
public static void UnionActivity<T>(IDbSet<T> source, IQueryable<DAL.Period> jointSource,
string product, string activity, int StartPeriod, double EndPeriod, ref List<PivotViewModel> unionSet) where T : class, IEntity
{
unionSet = unionSet.Union(source.Where(p => p.Product == product && p.PeriodID >= StartPeriod && p.PeriodID <= EndPeriod)
.Join(jointSource, c => c.PeriodID, o => o.PeriodID, (c, o) => new { c, o })
.Select(b => new PivotViewModel
{
Product = b.c.Product,
PeriodID = b.c.PeriodID,
SiteID = b.c.SiteID,
Value = b.c.Value,
Activity = activity,
PeriodStart = b.o.Period_Start,
PeriodEnd = b.o.Period_End,
PeriodDescription = b.o.Display
})).ToList();
}
I tried following but there is syntax error :
List<PivotViewModel> activityResult1 = new List<PivotViewModel>();
ViewerModel.UnionActivity<System.Data.Entity.IDbSet<DAL.OMS_Planned_Receipts)>(db.OMS_Planned_Receipts, db.Periods, product, "Planned Receipts", StartPeriod, iEndPeriod, ref activityResult1);