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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:49:52+00:00 2026-06-17T08:49:52+00:00

I have a pretty simple piece of code: var list = new List<MyType>(); list

  • 0

I have a pretty simple piece of code:

var list = new List<MyType>();
list = MyItems.Where(x => x.Name.ToLower().Contains(e.Text.ToLower())).ToList();
for (int i = itemOffset; i < endOffset; i++)
{
    combo.Items.Add(new ComboItem(list[i].Name, list[i].Id.ToString()));
}

What I want to do is to make this a generic function that accepts list of any type and accepts two strings as properties. (In this case two properties that we are looking at are Name and Id.

private void MakeCombo<T>(Combo combo, IEnumerable<T> lst, string Field1, string Field2)
{
    var list = list = lst.Where(x => x.Field1.ToLower().Contains(searchText.ToLower())).ToList(); //How make this to work???

    for (int i = itemOffset; i < endOffset; i++)
    {
        combo.Items.Add(new ComboItem(list[i].Field1, list[i].Field2.ToString())); //How make this to work???
    }
}

Now, I do not understand how can I access properties of generic list by string names (Field1 and Field2).

  • 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-17T08:49:53+00:00Added an answer on June 17, 2026 at 8:49 am

    One way of solving this problem, if you don’t have the ability of creating an interface and ensuring all of the items in the sequence implement that interface (as shown in this other answer), is to use delegates. By passing in two functions, each of which take a T as input, and return a string or object as the output, you can mimic that functionality:

    private void MakeCombo<T>(Combo combo, IEnumerable<T> sequence
        , Func<T, string> field1Selector, Func<T, object> field2Selector)
    {
        var foundItems = sequence.Where(item => field1Selector(item).ToLower()
            .Contains(searchText.ToLower()));
    
        foreach(var item in foundItems)
        {
            combo.Items.Add(new ComboItem(field1Selector(item), field2Selector(item).ToString()));
        }
    }
    

    From the calling side it might look something like this (to use your first code snippet as an example:

    MakeCombo(combo, list, item => item.Name, item => item.Id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have pretty simple piece of code var propertyBuilder = typeBuilder.DefineProperty(upperName, PropertyAttributes.None, propertyType, new[]
I have the following piece of code, executing a pretty simple MySQL query: $netnestquery
I have this simple piece of code in c++: int main(void) { string text
I have pretty simple jquery code : $(document).ready(function(){ $('img.marqFl').on({ mouseenter: function() { $(this).animate({height: 300},
I have a pretty simple Linq to XML query: var q = from c
I have a pretty simple problem. Basically I have an array called $list that
I have pretty simple application with text entry and button. When user press on
I have a simple piece of code that extracts a float from a FORTRAN-generated
So I'm pretty new to android development and have been trying to piece together
i have pretty simple simple question (i hope so). How do i change the

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.