我有以下Linq查詢,其中我從兩個不同的表中檢索具有相同過濾器和連接的數據;最後結合結果集。簡化我的Linq查詢和工會
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);
我需要對另外8個表執行相同的操作,最後將它們的結果集合在一起。我所有的表都有相同的模式。我希望用一個委託或方法來簡化它。請指教。
同時請注意,我使用EF 5 IDBSet過濾按applying global filters article
我覺得下面的方法應該是,但我不知道我應該如何調用它。
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();
}
我嘗試以下,但有語法錯誤:
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);
什麼是OMS_System_Forecast和OMS_Planned_Receipts的集合類型?這些是同一類型的集合嗎? – loopedcode 2014-09-19 16:44:31
你看看這個答案嗎? http://stackoverflow.com/a/23178704/1798889似乎是真正接近你在找什麼。 – CharlesNRice 2014-09-19 16:49:47
嗨@loopedcode;是的,它們是同一類型但不同表格的集合。 – 2014-09-19 16:59:44