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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:04:52+00:00 2026-06-05T03:04:52+00:00

I have the following code -> something like Select case using LINQ – This

  • 0

I have the following code -> something like Select case using LINQ –
This is not the actual code [picked from internet].
Just trying to show the Select Case concept using LINQ.
In my code, based on the conditions I am creating new List objects

Int32[] numbers = new Int32[] { 1, 2, 1, 3, 1, 5, 3, 1 };

var numberText =
(
    from n in numbers
    where n > 0
    select new
    {
        Number = n,
        Text = 
        (
            n == 1 ? "One" :
            n == 2 ? "Two" :
            n == 3 ? "Three" : "Unknown"
        )
    }
);

But, the page here says – “Replace condition with polymorphism”.

So, the question is whether such kind of implementation is a code smell and should it always be strictly avoided ? LINQ seems like an obvious choice while looping through collections and creating new objects conditionally. May be its wrong ?

  • 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-05T03:04:54+00:00Added an answer on June 5, 2026 at 3:04 am

    There is a fundemental difference. LINQ is an API that allows you to query collections (as you have demonstrated in your question). A switch (or other conditional) statement is used to control program flow.

    The page you are referring to is talking about refactoring code when you have a lot of conditional statements controlling program flow based on some type – in this case it suggests that you may want to replace all the conditionals with some poly-morphism. So in this case there is a code smell. But using conditionals in a LINQ statement is OK – there are probably many ways to structure your LINQ – some will perform better than others, and some will read better than others, but I would not say using conditionals in LINQ is a code smell.

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

Sidebar

Related Questions

I have something like the following code:: <xsl:variable name=sample> <xsl:copy-of select=//foo> <xsl:copy-of select=//bar> </xsl>
I have something like the following code: template<typename T1, typename T2, typename T3, typename
I have following code.. $query = SELECT quote, author FROM quotes ORDER BY id
I have the following code: $sql_latest = SELECT * FROM tbl_latest ORDER BY id
All, I have the following code: $fetch = mysql_query(SELECT * FROM calendar_events where event_status='booked'
I have a SQL query that is something like SELECT SUM(price) FROM budget GROUP
I have code using QuickForm that creates a select widget with the following: $form->addElement(
I have following SQL code: SELECT m.email FROM members as m WHERE exists (
I have following code: public static void ProcessStep(Action action) { //do something here if
I have following code if (rs != null) { out.println(result set has got something);

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.