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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:51:23+00:00 2026-05-25T22:51:23+00:00

I need a way to create an extension method off of an IEnumerable that

  • 0

I need a way to create an extension method off of an IEnumerable that will allow me to return a list of SelectListItem’s.

For example

    public class Role
    {
        public string Name {get;set;}
        public string RoleUID {get;set;}
    }
    IEnumerable<Role> Roles = .../*Get Roles From Database*/
    var selectItemList = Roles.ToSelectItemList(p => p.RoleUID,r => r.Name);

this would give me a SelectItemList with the Name being the display, and the RoleUID being the value.

IMPORTANT I want this to be generic so I can create it with any two properties off of an object, not just an object of type Role.

How can I do this?

I imagine something like the following

 public static List<SelectItemList> ToSelectItemList<T,V,K>(this IEnumerable<T>,Func<T,V,K> k)

or something, I obviously know that would be incorrect.

  • 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-25T22:51:24+00:00Added an answer on May 25, 2026 at 10:51 pm

    Why not just combine the existing Select and ToList methods?

    var selectItemList = Roles
      .Select(p => new SelectListItem { Value = p.RoleUID, Text = p.Name })
      .ToList();
    

    If you want to specifically put it into one method then you could define ToSelectListItem as a combination of those two methods.

    public static List<SelectListItem> ToSelectListItem<T>(
      this IEnumerable<T> enumerable,
      Func<T, string> getText,
      Func<T, string> getValue) {
    
      return enumerable
        .Select(x => new SelectListItem { Text = getText(x), Value = getValue(x) })
        .ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an extension method for a GraphicsPath object that determines if
Need a way to allow sorting except for last item with in a list.
I need a way to create an object from a location value. For example
I need to create an extension of a Flex component, which (obviously) means that
I am trying to create an extension method that i can forward a IList
I need to create an extension method to array class, but this extension method
I need to know if there is a way to create a custom shortcut
For a school project, I need to create a way to create personnalized queries
I need a way to bind POJO objects to an external entity, that could
I need a way to represent information about Weeks and Days as such: W1:

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.