Solved

How to pass IDbSet to generic method

Posted on 2014-09-23
13
251 Views
Last Modified: 2016-02-16
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
Comment
Question by:ran_deep
  • 7
  • 6
13 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40338939
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
 
LVL 2

Author Comment

by:ran_deep
ID: 40338954
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40338961
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
 
LVL 2

Author Comment

by:ran_deep
ID: 40339001
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40339018
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
 
LVL 2

Author Comment

by:ran_deep
ID: 40339049
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40339102
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
 
LVL 2

Author Comment

by:ran_deep
ID: 40339155
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40339165
Where did Ioms_Activity come from? I thought we were dealing with IEntity?
0
 
LVL 2

Author Comment

by:ran_deep
ID: 40339271
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40339450
OK, but your method requires IEntity:

public static void UnionActivity<T>(...) where T : class, IEntity
0
 
LVL 2

Author Comment

by:ran_deep
ID: 40339497
I have changed it to :
  public static void UnionActivity<T>(...) where T : class, DAL.Models.Ioms_Activity
   
0
 
LVL 2

Author Closing Comment

by:ran_deep
ID: 40341159
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Asp/Net Validation date Issue 9 38
How do I get the id from URL? 19 48
Data Saving 5 36
String manipulation 15 49
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now