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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:00:19+00:00 2026-05-15T16:00:19+00:00

i have the following code which has some duplication private List<SelectListItem> GetDeskList(int deskId) {

  • 0

i have the following code which has some duplication

    private List<SelectListItem> GetDeskList(int deskId)
    {
        List<Desk> apps = Model.GetDesks();

        List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
        {
            Selected = c.Id == deskId,
            Text = c.Name,
            Value = c.Id.ToString()
        }).ToList();
        dropdown.Insert(0, new SelectListItem());
        return dropdown;
    }

    private List<SelectListItem> GetRegionList(int regionId)
    {
        List<Region> apps = Model.GetRegions();

        List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
        {
            Selected = c.Id == regionId,
            Text = c.Name,
            Value = c.Id.ToString()
        }).ToList();
        dropdown.Insert(0, new SelectListItem());
        return dropdown;
    }

and a few more like it with a similar pattern. what is the best way to refactor this to avoid duplication

  • 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-15T16:00:19+00:00Added an answer on May 15, 2026 at 4:00 pm

    If you can change your models to implement a common interface (or inherit from a common base class) then you might be able to do something like this:

    var desks = GetList(123, () => Model.GetDesks());
    
    var regions = GetList(456, () => Model.GetRegions());
    
    // ...
    
    private List<SelectListItem> GetList<T>(int id, Func<List<T>> getApps)
        where T : IDropdownItem
    {
        List<T> apps = getApps();
    
        List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
            {
                Selected = c.Id == id,
                Text = c.Name,
                Value = c.Id.ToString()
            }).ToList();
    
        dropdown.Insert(0, new SelectListItem());
        return dropdown;
    }
    
    public interface IDropdownItem
    {
        int Id { get; }
        string Name { get; }
    }
    
    public class Desk : IDropdownItem { /* ... */ }
    
    public class Region : IDropdownItem { /* ... */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which obviously has some duplication. I'm sure this could
We have a lot of class code which has some boilerplate like the following:
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
I have a javascript file which has some code somewhat like this: var Slide
I have had some long standing code which has worked fine for quite some
I have the following code which does nothing but reading some values from a
I have written a HTML page which has following code. <div id=adsListContainer style=margin: 5px;>
Some scenarios to ponder. There is a legacy code which has following implementation Example1
Following the post , if I have header file,which has some functions implementations in
I have a C++ application which has the following structure: Class AAA: has some

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.