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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:37:24+00:00 2026-05-28T04:37:24+00:00

I know I can create a generic method that I can call from other

  • 0

I know I can create a generic method that I can call from other methods similar to the following, but it seems like a waste to have a seperate method when all I want to do is have a variable within a method be defined as generic:

private List<T> OrderedEntityList<T>(IRepository<T> repository) where T : Entity, IOrderedEntity
{
  return repository.All().OrderBy(x => x.DisplayOrder).ToList();
}

public SomeMethod()
{
  entityList = OrderedEntityList(_memberRepository);
  foreach (var entityRecord in entityList)
  {
    //... do work ...
  }
}

What I want is to do something like the following, where I can pass in the name of the repository as a parameter, but as it is, this doesn’t compile.

public SomeMethod(string repositoryName)
{
  List<T> entityList where T : Entity, IOrderedEntity = repository<T>.All().OrderBy(x => x.DisplayOrder).ToList();

  foreach (var entityRecord in entityList)
  {
    //... do work ...
  }
}

Any thoughts on how do to this?

— EDIT —

Thanks all for the quick feedback. Looks like I need to provide some additional information. Basically I renamed my method to make it easier for an example, but in reality, what I am trying to do is have a generic action in an MVC controller that can be used to display a list of entities (that all inherit from Entity and IOrderedEntity) which might include members, statuses, roles, etc. I am trying to make my MVC controller actions reusable regardless of the type of underlying repository (as long as each implements the same interface). Here’s my actual List method in my controller that I am trying to make “generic”.

public ActionResult List(string entity)
{
  //TODO: Find out how to declare this as a generic list instead of tying it to MemberStatus
  List<MemberStatus> entityList; 
  EntityIndexViewModel<OrderedEntityViewModel> indexViewModel;

  //TODO: Possibly move this switch statement to an EntityFactory (once I find out how...)
  switch (entity)
  {
    case "Status":
      indexViewModel = new EntityIndexViewModel<OrderedEntityViewModel>();
      //TODO: This is another line that I think is a waste to have as seperate method... but maybe I'm wrong
      entityList = OrderedEntityList(_memberStatusRepository);
      ViewBag.EntityName = "Member Statuses";
      break;
    default:
      throw new Exception("Entity is not valid");
  }

  foreach (var entityRecord in entityList)
  {
    var viewModel = new OrderedEntityViewModel();
    viewModel.Id = entityRecord.Id;
    viewModel.Name = entityRecord.Name;
    viewModel.DisplayOrder = entityRecord.DisplayOrder;
    viewModel.HasChildRecordsAssigned = _memberRepository.All().Any(m => m.StatusId == entityRecord.Id);
    indexViewModel.EntityList.Add(viewModel);
  }
  return View("List", indexViewModel);
}
  • 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-05-28T04:37:25+00:00Added an answer on May 28, 2026 at 4:37 am

    You could do something like this:

    public SomeMethod<T>(string repositoryName) where T : Entity, IOrderedEntity
    {
        List<T> entityList = repository<T>.All().OrderBy(x => x.DisplayOrder).ToList();
    
        foreach (var entityRecord in entityList)
        {
            //... do work ...
        }
    }
    

    But depending on the rest of your code, maybe what you really want is to take advantage of generic type variance in C# 4.0.

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

Sidebar

Related Questions

I know that we can't overload operator with other meaning, we can't create new
I have an interesting problem. I'd like to create a generic class that can
I'm using python 3.1.1. I know that I can create byte objects using the
I know that horizontal partitioning...you can create many tables. How can you do this
Does anyone know how I can Dynamically create an installation package that installs files
I'd like to create a structured type in Moose that can be used as
I want to know if i can create a custom google maps application,on which
I need to know if I can create a file in a specific folder,
I want to know if PHPExcel can: Create Excel spreadsheets with embeded image. The
I want to know if I can create a WebBrowser through code in Vb.NET,

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.