如何将IDbSet传递给泛型方法

时间:2014-09-23 05:03:12

标签: c# asp.net-mvc linq generics entity-framework-5

基于其他示例,我创建了一个通用方法(UnionActivity)来处理LINQ查询的查询和联合集。我将IDbSet结果传递给应添加到PivotViewModel列表的方法。我的所有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

1 个答案:

答案 0 :(得分: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>(

您可能有其他错误,但这是导致您当前错误的原因。