Solved

How to pass IDbSet to generic method

Posted on 2014-09-23
13
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 75

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 75

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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 75

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
 
LVL 75

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 75

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 75

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

695 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