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

The Archive Base Latest Questions

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

EDIT 1: Forgot to add the nested property curve ball. UPDATE: I have chosen

  • 0

EDIT 1: Forgot to add the nested property curve ball.

UPDATE: I have chosen @mtazva’s answer as that was the preferred solution for my specific case. In retrospect, I asked a general question with a very specific example and I believe that ended up confusing everyone (or maybe just me) as to what the question was exactly. I do believe the general question has been answered as well (see the Strategy pattern answers and links). Thanks everyone!

Large switch statements obviously smell and I have seen some links on how you could do this with a dictionary that maps to functions. But I’m wondering if there is a better (or smarter way) to do this? In a way, this is a question I’ve always sort of had rolling around in the back of my head but never really had a good solution to.

This question stemmed from another question I asked earlier: How to select all the values of an object's property on a list of typed objects in .Net with C#

Here is an example class I’m working with (from an external source):

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

public class GameInfo
{
    public int UserId { get; set; }
    public int MatchesWon { get; set; }
    public long BulletsFired { get; set; }
    public string LastLevelVisited { get; set; }
    public NestedGameInfoObject SuperCoolNestedGameInfo { get; set; }
    // thousands more of these
}

Unfortunately, this is coming from an external source… imagine a HUGE data dump from Grand Theft Auto or something.

And I want to get just a small cross section of a list of these objects. Imagine we want to be able to compare you with a bunch of your friends’ game info objects. An individual result for one user would look like this:

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
}

And an example of what I want to replace with something more manageable (believe me, I DON’T want to be maintaining this monster switch statement):

const int MATCHES_WON = 1;
const int BULLETS_FIRED = 2;
const int NESTED_INT = 3;

public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
  var output = new List<MyResult>();

  switch(input)
  {
    case MATCHES_WON:
        output = gameInfos.Select(x => new MyResult()
         {
            UserId = x.UserId, 
            ResultValue = x.MatchesWon.ToString()
         }).ToList<MyResult>();
      break;

    case BULLETS_FIRED:
        output = gameInfos.Select(x => new MyResult()
         {
            UserId = x.UserId, 
            ResultValue = x.BulletsFired.ToString()
         }).ToList<MyResult>();
      break;

    case NESTED_INT:
        output = gameInfos.Select(x => new MyResult()
         {
            UserId = x.UserId, 
            ResultValue = x.SuperCoolNestedGameInfo.NestedIntValue.ToString()
         }).ToList<MyResult>();
      break;

    // ad nauseum
  }

  return output;
}

So the question is are there any reasonable ways to manage this beast? What I’d really like is a dynamic way to get this info in case that initial object changes (more game info properties are added, for instance). Is there a better way to architect this so it’s less clumsy?

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

    I think your first sentence eluded to what is probably the most reasonable solution: some form of dictionary mapping values to methods.

    For example, you could define a static Dictionary<int, func<GameInfo, string>>, where each value such as MATCHES_WON would be added with a corresponding lambda that extracts the appropriate value (assuming your constants, etc are defined as shown in your example):

    private static Dictionary<int, Func<GameInfo, string>> valueExtractors =
        new Dictionary<int, Func<GameInfo, string>>() {
            {MATCHES_WON,   gi => gi.MatchesWon.ToString()},
            {BULLETS_FIRED, gi => gi.BulletsFired.ToString()},
            //.... etc for all value extractions
        };
    

    You can then use this dictionary to extract the value in your sample method:

    public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
    {
      return gameInfo.Select(gi => new MyResult()
             {
                UserId = gi.UserId, 
                ResultValue = valueExtractors[input](gi)
             }).ToList<MyResult>();
    }
    

    Outside of this option, you could potentially have some sort of file/database/stored lookup with the number and the property name, then use reflection to extract the value, but that would obviously not perform as well.

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

Sidebar

Related Questions

EDIT: See my answer below--> I am wanting to have a view that when
EDIT: Forgot to remind the reader that I remembered to set templateSettings as follows:
EDIT Sorry I forgot the most important part here. Each key can have more
Edit : Note that, as Daniel and latkin noted in an answer and a
EDIT: I forgot to add semi colons. Now there is another problems. I'm getting
I have a website that is already completed in ASP.NET. I need to add
EDIT: I forgot to add the loop part of the second code. Looking at
EDIT: forgot a line of code that is likely causing the issue (night's sleep
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: I was an idiot. I simply had an image that was vertically long,

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.