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

  • Home
  • SEARCH
  • 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 7762099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:18:24+00:00 2026-06-01T14:18:24+00:00

In my project, I have 2 Entity objects (delegate for 2 tables in db)

  • 0

In my project, I have 2 Entity objects (delegate for 2 tables in db) as below:

  • Tbl_Person {ID, Name}
  • Tbl_Class {ID, Name, PersonID}

In DAL, I create 2 classes for these Entities, and write function GetList():
– Class Person:

public List<Tbl_Person> GetList()
{
    using (var db = DatabaseHelper.DataContext())
    {
        try
        {
            var _t = (from info in db.**tbPerson** 
                      select info).ToList();
            return _t.ToList<**Tbl_Person**>();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

Class MyClass:

public List<Tbl_Class> GetList()
{
    using (var db = DatabaseHelper.DataContext())
    {
        try
        {
            var _t = (from info in db.**tbClass** 
                      select info).ToList();
            return _t.ToList<**Tbl_Class**>();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

If I use 2 classes, I can use GetList() to get list object correctly.
But it seems we have duplicate function GetList() here. What I want just is only 1 function GetList() like this:

public List<T> GetList()
{
    using (var db = DatabaseHelper.DataContext())
    {
        try
        {
            var _t = (from info in db.**????????** 
                      select info).ToList();
            return _t.ToList<**T**>();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
  • 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-01T14:18:26+00:00Added an answer on June 1, 2026 at 2:18 pm

    Firstly, get rid of the pointless try/catch blocks. Each of those methods is twice as long as it needs to be, because you’re catching exceptions and just rethrowing them – but losing information at the same time! (If you must rethrow, use just throw; instead of throw ex;.)

    Secondly, there’s absolutely no point in using a query expression here. You’re just interested in the whole table. (Are you sure you want to fetch the whole table on each call, by the way?)

    Thirdly, assuming these are proper strongly-typed data contexts, I wouldn’t expect you to have to specify the type argument for ToList in the first place.

    So your two methods can *actually be reduced to:

    // In MyClass
    public List<Tbl_Class> GetList()
    {
        using (var db = DatabaseHelper.DataContext())
        {
            return db.tbClass.ToList();
        }
    }
    
    // In Person
    public List<Tbl_Person> GetList()
    {
        using (var db = DatabaseHelper.DataContext())
        {
            return db.tbPerson.ToList();
        }
    }
    

    Now you could remove the redundancy here using DataContext.GetTable<TEntity>:

    // In DatabaseHelper:
    public static List<T> GetList<T>()
    {
        using (var db = DataContext())
        {
            return db.GetTable<T>().ToList();
        }
    }
    
    // In MyClass
    public List<Tbl_Class> GetList()
    {
        return DatabaseHelper.GetList<Tbl_Class>();
    }
    
    // In Person
    public List<Tbl_Person> GetList()
    {
        return DatabaseHelper.GetList<Tbl_Person>();
    }
    

    It’s not really clear whether you need the methods in the individual classes…

    (In addition to all of this, I’d strongly encourage you to rename your types to remove the Tbl_ prefix from the mapping. It looks pretty horrible in code, IMO.)

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

Sidebar

Related Questions

I have converted an Entity framework project to use POCO objects by removing the
I have an entity framework project setup. It it I have four tables and
I have an IQueryable whose Entity Framework 4 objects I would like to project
In the project I'm working on now, we have base Entity class that looks
I have one project which has RIAService with entity framework that is referenced to
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have a problem adding ADO.Net Entity Data Model to my existing project, or
I'm using Entity Framework in my project, and I have the problem that, once
I have the following table structure, which is imported into an Entity Framework project:
I'm working on a project in which we have a database, data layer (entity

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.