Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

How to pass IDbSet to generic method

Based on other examples, I have created a generic method (UnionActivity) to handle querying and union set of LINQ queries. I will pass IDbSet results into the method which should add in to PivotViewModel list. All my IDbSet have same schema type.

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

Open in new window

Following is declaration of my data context

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

Open in new window

I'm trying to call the method as following, but its giving me syntax error :

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

Open in new window

The error I'm getting is :

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

Open in new window

0
ran_deep
Asked:
ran_deep
  • 7
  • 6
1 Solution
 
käµfm³d 👽Commented:
You don't need to include "IDbSet" in the angle brackets:

ViewerModel.UnionActivity<DAL.OMS_Planned_Receipts>(...

Open in new window


Your generic method says that the first parameter will be an IDbSet<Something>. You just need to supply the "Something".
0
 
ran_deepAuthor Commented:
That doesn't work. It gives me following error:

Error      5      The type 'DAL.OMS_Planned_Receipts' cannot be used as type parameter 'T' in the generic type or method 'Net.Areas.OMS.Models.ViewerModel.UnionActivity<T>(System.Data.Entity.IDbSet<T>, System.Linq.IQueryable<DAL.Period>, string, string, int, double, ref System.Collections.Generic.List<Net.Areas.OMS.Models.PivotViewModel>)'. There is no implicit reference conversion from 'DAL.OMS_Planned_Receipts' to 'Net.Areas.OMS.Models.IEntity'.
0
 
käµfm³d 👽Commented:
Does OMS_Planned_Receipts implement the IEntity interface? Whatever "Something" you pass has to implement that interface, based on the type constraint that exists on that generic method.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ran_deepAuthor Commented:
Actually following is my entity data model declaration :

namespace DAL
{
    using System;
    using System.Collections.Generic;
    
    public partial class OMS_Planned_Receipts
    {
        public short TenantID { get; set; }
        public string Product { get; set; }
        public string SiteID { get; set; }
        public int PeriodID { get; set; }
        public Nullable<double> Value { get; set; }
        public Nullable<double> Value2 { get; set; }
        public System.DateTime UpdateDate { get; set; }
        public string UpdateBy { get; set; }
        public System.DateTime CreatedDate { get; set; }
        public string CreatedBy { get; set; }
    
        public virtual OMS_Product OMS_Product { get; set; }
        public virtual Period Period { get; set; }
        public virtual Site Site { get; set; }
        public virtual Tenant Tenant { get; set; }
    }
}

Open in new window


Hence I created a replica of this class as interface in my model:

    public interface IEntity
    {
        public short TenantID { get; set; }
        public string Product { get; set; }
        public string SiteID { get; set; }
        public int PeriodID { get; set; }
        public Nullable<double> Value { get; set; }
        public Nullable<double> Value2 { get; set; }
        public System.DateTime UpdateDate { get; set; }
        public string UpdateBy { get; set; }
        public System.DateTime CreatedDate { get; set; }
        public string CreatedBy { get; set; }

        public virtual OMS_Product OMS_Product { get; set; }
        public virtual Period Period { get; set; }
        public virtual Site Site { get; set; }
        public virtual Tenant Tenant { get; set; }
    }

Open in new window


So I'm not doing it right here. Please advise, how should I declare the interface.
0
 
käµfm³d 👽Commented:
You (should) just need to tack on that interface to the class itself (in addition to what I mentioned earlier):

public partial class OMS_Planned_Receipts : IEntity

Open in new window

0
 
ran_deepAuthor Commented:
The public partial class OMS_Planned_Receipts is part of auto created class from DbContext and usually its not recommended to modify this class since it will be overwritten if code is regenerated. Is there another way around?
0
 
käµfm³d 👽Commented:
Since the class is marked as partial, you can include another partial definition of the class--in the same namespace--that has the interface definition. Create an additional code file:

e.g.

namespace DAL
{
    public partial class OMS_Planned_Receipts : IEntity { }
}

Open in new window

0
 
ran_deepAuthor Commented:
That doesn't work too. I created following in separate class :

    public interface Ioms_Activity
    {
        short TenantID { get; set; }
        string Product { get; set; }
        string SiteID { get; set; }
        int PeriodID { get; set; }
        Nullable<double> Value { get; set; }
        Nullable<double> Value2 { get; set; }
        System.DateTime UpdateDate { get; set; }
        string UpdateBy { get; set; }
        System.DateTime CreatedDate { get; set; }
        string CreatedBy { get; set; }
    }

    public partial class OMS_Planned_Receipts : Ioms_Activity { }

Open in new window


Am getting following errors on all fields :

Error      1      'DAL.Models.OMS_Planned_Receipts' does not implement interface member 'DAL.Models.Ioms_Activity.CreatedBy'
...
Error      7      'DAL.Models.OMS_Planned_Receipts' does not implement interface member 'DAL.Models.Ioms_Activity.PeriodID'
0
 
käµfm³d 👽Commented:
Where did Ioms_Activity come from? I thought we were dealing with IEntity?
0
 
ran_deepAuthor Commented:
IEntity was in my current web project. So I created Ioms_Activity in the DAL project to inherit my partial class. Anyway, both have same declaration.
0
 
käµfm³d 👽Commented:
OK, but your method requires IEntity:

public static void UnionActivity<T>(...) where T : class, IEntity
0
 
ran_deepAuthor Commented:
I have changed it to :
  public static void UnionActivity<T>(...) where T : class, DAL.Models.Ioms_Activity
   
0
 
ran_deepAuthor Commented:
This was solution to my initial error, but it didn't resolve entirely. Something was still lacking in my approach, hence I had to change my approach.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now