繁体   English   中英

如何将IDbSet传递给泛型方法

[英]How to pass IDbSet to generic method

基于其他示例,我创建了一个通用方法(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

在您的代码中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>(

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM