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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:14:07+00:00 2026-05-22T12:14:07+00:00

I’m new to C# and I have the following 3 methods. These methods allow

  • 0

I’m new to C# and I have the following 3 methods. These methods allow the caller to retrieve differents properties of the table by specifying a lambda expression on the given method. However, I have the feeling an expert would combine them even further into a single generic method. If this is possible, please let me know how.

private KeyValuePair<T, int> GetHeaderProperty<T>(Func<Header, T> Property, 
                                                  Source ds)
{
    Func<Source, Header> GetValue =
        a => Books.Where(str => str.BookId == a.DiscardBookId)
                 .First().Header;
    return new KeyValuePair<T,int>(Property(GetValue(ds)), 0);
} 

private KeyValuePair<T, int> GetBookProperty<T>(Func<Book, T> Property,
                                               Source ds)
{
    Func<Source, Book> GetValue = 
        a => Books.Where(str => str.BookId == a.DiscardBookId).First();

    return new KeyValuePair<T, int>(Property(GetValue(ds)), 0);
} 

private KeyValuePair<T, int> GetFleetProperty<T>(Func<Fleet, T> Property,
                                                 Source ds)
{
    Func<Source,Fleet> GetValue =
        a => Books.Where(str => str.BookId == a.DiscardBookId).First()
                 .Header.Fleet;

    return new KeyValuePair<T,int>(Property(GetValue(ds)), 0);
} 
  • 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-22T12:14:08+00:00Added an answer on May 22, 2026 at 12:14 pm

    I think the following will be equivalent to calling all three methods in a row and adding the results to a list:

    private IEnumerable<KeyValuePair<T, int>> GetFleetProperty<T>(
                                                  Func<Book, T> PropertyBook,
                                                  Func<Header, T> PropertyHeader,
                                                  Func<Fleet, T> PropertyFleet,
                                                  Source ds)
    {
        Func<Source,Fleet> GetValue =
            a => Books.Where(str => str.BookId == a.DiscardBookId).First();
    
        var book = GetValue(ds);
        var result = new List<KeyValuePair<T, int>>();
        result.Add(new KeyValuePair<T, int>(PropertyBook(book), 0);
        result.Add(new KeyValuePair<T, int>(PropertyHeader(book.Header), 0);
        result.Add(new KeyValuePair<T, int>(PropertyFleet(book.Header.Fleet), 0);
        return result;
    } 
    

    UPDATE:
    You could also create a method like this:

    private KeyValuePair<T, int> GetProperty<T, TProperty>(
                                     Func<TProperty, T> Property, 
                                     Func<Book, TProperty> GetProperty, 
                                     Source ds)
    {
        Func<Source, Header> GetValue =
            a => Books.Where(str => str.BookId == a.DiscardBookId)
                     .First();
        var book = GetValue(ds);
        return new KeyValuePair<T,int>(Property(GetProperty(book)), 0);
    } 
    

    You would call it like this for Header:

    GetProperty(xyz, b => b.Header, ds);
    

    You would call it like this for Book:

    GetProperty(xyz, b => b, ds);
    

    You would call it like this for Fleet:

    GetProperty(xyz, b => b.Header.Fleet, ds);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.