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 7778143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:19:20+00:00 2026-06-01T18:19:20+00:00

I have two similar methods that basically does the same thing only with different

  • 0

I have two similar methods that basically does the same thing only with different objects.
What’s the best way to make a generic method out of this if possible?

The two objects:

public class StoreObject {
  int Key;
  string Address;
  string Country;
  int Latitude;
  int Longitude;
}

public class ProjectObject {
  int ProjectKey;
  string Address;
  string Description;
}

The two methods that I potentially want to make into a generic:

public StoreObject GetStoreByKey(int key)
{
  using (DBEntities dbe = new DBEntities())
  {
    StoreObject so = new StoreObject();
    var storeObject = (from s in dbe.StoreTables
                       where s.Key == key
                       select s).First();

    so.Key = storeObject.key;
    so.Address = storeObject.address;
    so.Country = storeObject.country;
    so.Latitude = storeObject.latitude;
    so.Longitude = storeObject.longitude;

    return so;
  }
}

public ProjectObject GetProjectByKey(int projectKey)
{
  using (DBEntities dbe = new DBEntities())
  {
    ProjectObject po = new ProjectObject();
    var projectObject = (from p in dbe.ProjectTables
                       where p.ProjectKey == projectKey
                       select p).First();

    po.Key = projectObject.p_key;
    po.Address = projectObject.p_address;
    po.Description = projectObject.p_description;

    return po;
  }
}

I must note that:
– I have no control over how the table fields are named (ie. p_description).
– StoreTable in the DB, for example, may have other properties (like telephone, postal code, etc) but I’m only interested in showing what I’ve shown in the code.
– The same goes for the ProjectTable.

  • 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-01T18:19:22+00:00Added an answer on June 1, 2026 at 6:19 pm

    Well, the tricky part is that your entities have different properties, so using generics to populate the different properties within one method will not be worth it. But you can return the whole object and then just use the properties you are interested in.

    public T GetEntityByKey<T>(int key)
    {
      using (DBEntities dbe = new DBEntities())
      {
        return = dbe.StoreTables.Set<T>.Find(new object[] {key});
      }
    }
    

    And to use it

    StoreObject so  = GetEntityByKey<StoreObject>(123);
    if(so != null)
    {
        int lat = so.Latitude;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two very similar classes that do essentially the same thing. The only
I have the following two methods that (as you can see) are similar in
I have two similar methods that take a criteria object (dumb object with a
I have two click-events, that are nearly similar, but not quite. I am wondering
I have two very similar methods: public IQueryable<User> Find(Func<User, bool> exp) { return db.Users.Where(exp);
I have two classes (this is C#) that are very similar except they each
I have two very similar methods in Grails, something like calculate statistics by os
I have two data access objects that are reverse generated and jar'ed up for
assume that i have two sequential methods, methodA and methodB ( methodB is called
I have two ways of doing a similar thing and would like to know:

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.