2014-09-23 52 views
0

基於其他示例,我創建了一個泛型方法(UnionActivity)來處理查詢和聯合集LINQ查詢。我會將IDbSet結果傳遞給應該添加到PivotViewModel列表的方法。我所有的IDbSet都具有相同的模式類型。如何將IDbSet傳遞到泛型方法

namespace PScope.Net.Areas.OMS.Models 
{ 
    public interface IEntity 
    { 
     Int16 TenantID { get; set; } 
     string Product { get; set; } 
     string SiteID { get; set; } 
     int PeriodID { get; set; } 
     double? Value { get; set; } 
     double? Value2 { get; set; } 
     DateTime UpdateDate { get; set; } 
     string UpdateBy { get; set; } 
     DateTime CreatedDate { get; set; } 
     string CreatedBy { get; set; } 
    } 

    public static class ViewerModel 
    { 
     const string PScopeFilterContextKey = "DXFilterDataContext"; 

     public static EFDbContext db 
     { 
      get 
      { 
       if (HttpContext.Current.Items[PScopeFilterContextKey] == null) 
        HttpContext.Current.Items[PScopeFilterContextKey] = new EFDbContext(); 
       return (EFDbContext)HttpContext.Current.Items[PScopeFilterContextKey]; 
      } 
     } 

     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(); 
     } 
    } 

    public class PivotViewModel 
    { 
     [Display(Name = "Period ID")] 
     public Int32 PeriodID { get; set; } 

     [Display(Name = "Activity")] 
     public string Activity { get; set; } 

     [Display(Name = "Site")] 
     public string SiteID { get; set; } 

     [Display(Name = "Product")] 
     public string Product { get; set; } 

     [Display(Name = "Value")] 
     public double? Value { get; set; } 

     [Display(Name = "Start")] 
     public DateTime PeriodStart { get; set; } 

     [Display(Name = "End")] 
     public DateTime PeriodEnd { get; set; } 

     [Display(Name = "PeriodDescription")] 
     public string PeriodDescription { get; set; } 
    } 
} 

以下是我的數據上下文

public class EFDbContext : DbContext, IDataContext 
{ 
    public IDbSet<OMS_Planned_Receipts> OMS_Planned_Receipts { get; set; } 
    public IDbSet<OMS_System_Forecast> OMS_System_Forecast { get; set; } 
    public IDbSet<OMS_Sales_History> OMS_Sales_History { get; set; } 
} 

我想調用的方法如下,但它給我的語法錯誤的聲明:

 public ActionResult ProductPartial(string product) 
    { 

     var stockstatus = db.OMS_StockStatus.Where(t => t.Product == product); 

     double maxLeadTime = stockstatus.Max(a => a.LeadTime); 
     double iEndPeriod = EndPeriod + maxLeadTime; 

     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); 

     ViewerModel.UnionActivity<System.Data.Entity.IDbSet<DAL.System_Forecast>>(db.System_Forecast, db.Periods, product, "Forecast", StartPeriod, iEndPeriod, ref activityResult1); 
     return PartialView("ProductPartial", activityResult1); 
    } 

我的錯誤得到的是:

Error 5 Argument 1: cannot convert from 'System.Data.Entity.IDbSet<PrimariusScope.DAL.OMS_Planned_Receipts>' to 'System.Data.Entity.IDbSet<System.Data.Entity.IDbSet<PrimariusScope.DAL.OMS_Planned_Receipts>>' 

Error 4 The best overloaded method match for 'PrimariusScope.Net.Areas.OMS.Models.ViewerModel.UnionActivity<System.Data.Entity.IDbSet<PrimariusScope.DAL.OMS_Planned_Receipts>>(System.Data.Entity.IDbSet<System.Data.Entity.IDbSet<PrimariusScope.DAL.OMS_Planned_Receipts>>, System.Linq.IQueryable<PrimariusScope.DAL.Period>, string, string, int, double, ref System.Collections.Generic.List<PrimariusScope.Net.Areas.OMS.Models.PivotViewModel>)' has some invalid arguments 
+0

並且哪些行表示錯誤,請在代碼中註明註釋。還有'UnionActivity ('似乎缺少它的最後一個'>'是另一個複製粘貼錯字,還是在你的代碼? – 2014-09-23 05:38:53

+0

@ScottChamberlain,抱歉,這是缺少我的代碼。原來)假設是>。如上所述的當前錯誤。 – 2014-09-23 06:00:10

回答

1

在您的代碼ViewerModel.UnionActivity<System.Data.Entity.IDbSet<DAL.OMS_Planned_Receipts>>(ViewerModel.UnionActivity<System.Data.Entity.IDbSet<DAL.System_Forecast>>(中都聲明第一個參數爲IDbSet<T>,因此不需要在泛型中再次說明。

分別將這兩個呼叫更改爲ViewerModel.UnionActivity<DAL.OMS_Planned_Receipts>(ViewerModel.UnionActivity<DAL.System_Forecast>(

您可能有其他錯誤,但這是導致您目前的錯誤。

+0

是的,你是對的。現在我得到錯誤5類型'DAL.OMS_Planned_Receipts'不能用作類型參數'T'在泛型類型或方法'Net.Areas.OMS.Models.ViewerModel.UnionActivity (System.Data.Entity。 IDbSet ,System.Linq.IQueryable ,string,string,int,double,ref System.Collections.Generic.List )'。沒有從'DAL.OMS_Planned_Receipts'到'Net.Areas.OMS.Models.Intity – 2014-09-23 13:53:26

+0

的隱式引用轉換請不要在評論中提出新問題。如果您現在有不同的問題,請提出一個新問題。請包括相關的代碼,消息的確切文本,並在新問題中指出錯誤發生在哪一行。 – 2014-09-23 14:02:12

相關問題