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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:19:52+00:00 2026-05-25T12:19:52+00:00

Ugh, how do I explain this one… Probably a simple question but my mind

  • 0

Ugh, how do I explain this one… Probably a simple question but my mind is fried.

Suppose I have this class:

public class NestedObject
{
    public string NestedName { get; set; }
    public int NestedIntValue { get; set; }
    public decimal NestedDecimalValue { get; set; }
}

public class SomeBigExternalDTO
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int SomeIntValue { get; set; }
    public long SomeLongValue { get; set; }
    public decimal SomeDecimalValue { get; set; }
    public string SomeStringValue { get; set; }
    public NestedObject SomeNestedObject { get; set; }
    // ... thousands more of these properties... inherited code
}

And the class I’d like to populate is here:

public class MyResult
{
    public int UserId { get; set; }  // user id from above object
    public string ResultValue { get; set; }  // one of the value fields from above with .ToString() executed on it
}

What I’d like to do is create a helper to return the property values (a cross section is the best way I could describe it I guess) of all instances in a list of this object:

var foo = new List<SomeBigExternalDTO>();
foo = GetMyListOfSomeBigExternalDTO();

public static List<MyResult> AwesomeHelper(List<SomeBigExternalDTO> input, SearchableProperty thePropertyIWant)
{
    // some magic needs to happen here...
}

The tricky part here is I want to dynamically pass in the property based on a link selector (I have no clue how to do this):

var output = AwesomeHelper(GetMyListOfSomeBigExternalDTO(), x => x.SomeIntValue);
var output2 = AwesomeHelper(GetMyListOfSomeBigExternalDTO(), x => x.SomeNestedObject.NestedIntValue); 

And this should return a list of MyResult objects with the UserId and SomeIntValue.ToString() corresponding to each item in the input list.

Wow, I really hope this makes sense. Please let me know if this is not clear I’ll provide more details. I’m really hoping this is something baked into the libraries that I’ve overlooked.

Any ideas on I’d accomplish this?

  • 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-25T12:19:52+00:00Added an answer on May 25, 2026 at 12:19 pm

    Often when trying to create a general purpose list operator you end up reimplementing what LINQ already offers you.

    Here’s the LINQ code for what you’re after (without an AwesomeHelper function):

    var results = list.Select(l => new MyResult()
    {
        UserId = l.UserId,
        ResultValue = l.SomeDecimalValue.ToString()
    }).ToList();
    

    Fairly simple.

    If you want to have an AwesomeHelper function as you requested then it looks like this:

    public static List<MyResult> AwesomeHelper(
        List<SomeBigExternalDTO> input,
        Func<SomeBigExternalDTO, object> selector)
    {
        return input
            .Select(i => new MyResult()
            {
                UserId = i.UserId,
                ResultValue = selector(i).ToString()
            })
            .ToList();
    }
    

    And the calling code look like this:

    var results = AwesomeHelper(list, x => x.SomeIntValue);
    

    To me, though, this is now less readable than the LINQ option. Now there is some magic being wrought and it’s hard to work out what.

    I have an alternative that will give you the best of both worlds.

    First, define an extension method called ToMyResult that maps a single SomeBigExternalDTO instance into a single MyResult with a field selector, like this:

    public static class AwesomeHelperEx
    {
        public static MyResult ToMyResult(
                this SomeBigExternalDTO input,
                Func<SomeBigExternalDTO, object> selector)
        {
            return new MyResult()
            {
                UserId = input.UserId,
                ResultValue = selector(input).ToString()
            };
        }
    }
    

    Now the calling code is crystal clear, flexible and concise. Here it is:

    var results = (
            from item in list
            select item.ToMyResult(x => x.SomeLongValue)
        ).ToList();
    

    I hope this helps.

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

Sidebar

Related Questions

UGH. Hi. I have a form. I'd like to know how/if I could submit
Ugh, got this unexpected error: Argument 1 passed to QMySqliDatabaseResult::__construct() must be an instance
I'm using old fashioned ASP.NET validation (ugh) for a checkout process. I have a
With StompChicken's corrections (I miscomputed one dot product, ugh!) the answer appears to be
I have the following problem with code folding: if I have a class with
Ugh - not sure why I'm having so much trouble with this. Writing a
I've found this h5validate plugin but having no luck with it when using it
Ugh! Any ideas? Have a form which tracks user activity for given time intervals.
I have a set of classes with static methods. Example member of class set:
Ugh ok I'm terrible at explaining things, so I'll just give you the quotes

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.