Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:22:00+00:00 2026-06-06T20:22:00+00:00

I am new to c# and now bit confused with Generics and Entity Framework.

  • 0

I am new to c# and now bit confused with Generics and Entity Framework. I have two tables in database, which I retrieve in my code using Entity Framework. On my aspx page, I have a grid which should populate the data based on what table user selects.

In future there will be more tables. So I wanted to write a factory pattern to get the source list for datagrid. I can not make it to work because i am very confused.

Here is my code for BaseClass and two child classes.

static class Factory
{
    public static ReportBase GetReport(string name)
    {
        switch (name)
        {
            case "Child1":
                return new Child1();
            case "Child2":
                return new Child1();
            default:
                return null;
        }
    }
}

//Base Class 
class ReportBase<T>
{
    public List<T> _list;
    public abstract void Load();
    public abstract List<T> Filter(DateTime statrtDate, DateTime endDate);
}

//Child 1
class Child1 : ReportBase
{
    public List<GetChild1> _list;
    public Child1(){}

    public override void Load()
    {
        //GetChild1 is the name of database table
        var info = from p in Context.GetChild1 select p;
        _list = info.ToList();
    }

    public List<GetChild1> Filter(DateTime startDate, DateTime endDate)
    {
        var filteredValues = from p in _list where p.UploadedDate <= startDate select p;
        return filteredValues.ToList();
    }
}

//Child 2
class Child2 : ReportBase
{
    public List<GetChild2> _list;
    public Child2() { }

    public override void Load()
    {
        //GetChild2 is the name of database table
        return  (from p in Context.GetChild2 select p).ToList();
    }

    public List<GetChild2> Filter(DateTime startDate, DateTime endDate)
    {
        return  (from p in _list where p.UploadedDate <= startDate select p).ToList();
    }
} 

Can Someone please correct the code accordingly? Do I have to use Generics here? I tried using it in BaseClass but it doesn’t work properly, because I have to fix my child classes accordingly, for which I have no clue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:22:01+00:00Added an answer on June 6, 2026 at 8:22 pm

    First of all, with generics you can write better code that is more readable and short.
    I think it is better not to use a factory class for repositories in the end you need to know which type you are dealing with it in your code such as:

    Child1 report = new Child1();
    report.SomeBehaviorInChild1(); // this is not achievable with base class.
    

    If you want to use this factory you can do the following:
    ReportBase report = Factory.GetReport();

    Secondly, it is a bad idea to let your list public because you don’t want to those who are using your class to create a new list from their code such as:
    report._list = new List();
    You don’t want this thing to happen to the objects in your class. So it is better to have your list private and depend only on the methods to return the data source of your report.
    private List _list;

    public List<T> GetDataSource()
    {
         return _list;
    }
    

    Thirdly, if you implement a generic reportbase you won’t need to write the child classes unless they have something special that the base doesn’t implement.
    Fourthly, the current implementation of Filter method is soooo bad because what you are doing here is getting all the records from database then filtering them in memory. This implementation is a bad practice. A better approach is to use the IQueryable which is a deffered execution object i.e. the result won’t be populated until you request it.

    public List<GetChild1> FilterExample()
    {
        IQueryable<GetChild1> result = _context.GetChild1;
        result = from p in result
                 where p.UploadDate < startDate;
                 select p;
        //until this moment the query is still not send to the database.
        result = result.OrderBy(p => p.UploadDate);
    
        //when you call the ToList method the query will be executed on the database and the list of filtered and sorted items will be returned. Notice the sorting and filtering is done in the database which is faster than doing it in memory
        List<GetChild1> populatedResult = result.ToList();
        return populatedResult;
    }
    

    So this was a better approach to your problem. I think it is good for you to read a book called “More Effective C# 2008” it talks about queryable and linq in general.
    Now if we apply this on your BaseReportClass we can get the following:

     //Base Class 
    class ReportBase<T> where T: class
    {
        private DbContext _context;
        public ReportBase(DbContext context)
        {
            _context = context;
        }
    
    
        //I think GetAll is a more suitable name for your method than load :D
        public IQueryable<T> GetAll()
        {
            return _context.Set<T>();
        }
    
        public IQueryable<T> Filter(Func<T,bool> filterFunction)
        {   
             var result = from p in GetAll()
                          where filterFunction(p)
                          select p;
             return result;
        }
    }
    

    Now to defining the additional behavior for the child classes.

    //Child 1
    class Child1 : ReportBase<GetChild1>
    {
       public ReportBase(DbContext context):base(context)
       {
    
       }
    
       public List<GetChild1> FilterOnStartDate(DateTime startDate)
       {
            //I don't know if you are familiar with lambda expressions but if you are not you can research it on the internet.
            //The parameter here is converted into a method that its parameter is "p" of type GetChild1
            // and the body is "p.UploadDate < startDate".
            // Note you can remove the type of the parameter because the compiler can predict it.
            return Filter((GetChild1 p) => p.UploadDate < startDate).ToList();
       }
    } 
    

    This is the code that uses your classes:

    Child1 report = new Child1(new GetChild1Entities());
    report.FilterOnStartDate(DateTime.Now);
    

    Hope this was useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that TWTweetComposeViewController is new to iOS 5 (which is now a bit
I'm quite new to MediaWiki, and now I have a bit of a problem.
new on rails and using windows for now,, i have web page that user
I'm new to Ruby on rails and am a bit confused. I have an
I'm using SQLite as my database, and I'm a little bit confused about how
I'm kind of new to this so a bit confused. I have a js
I see lot of new machines and laptops now having 64 bit hardware and
I am now trying to create new database manager under Fragment class. But unfortunately,
Actually i m little bit confused in Intent. Suppose i have three activities. A,b,c
I'm a bit confused with all these new File I/O classes in JDK7. Let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.