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

  • Home
  • SEARCH
  • 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 8623169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:13:15+00:00 2026-06-12T07:13:15+00:00

Overview I have a string delimited by commas, representing objects, and spaces, representing properties

  • 0

Overview

I have a string delimited by commas, representing objects, and spaces, representing properties of those objects:

string sOrderBy = "Column1 ASC, Column2 DESC";

I need to convert it to a List<OrderByColumn> where OrderByColumn is:

public class OrderByColumn
{
    public string ColumnName { get; set; }
    public bool IsAscending { get; set; }
}

Problem

sOrderBy.Split(',').Select(x => new OrderByColumn()
{
    ColumnName = x.Trim().Split(' ')[0].Trim(),
    IsAscending = x.Trim().Split(' ')[1].Trim() == "ASC" ? true : false
}).ToList<OrderByColumn>();

The above code works, but there’s some redundancy in calling x.Trim().Split(' ') more than once. (Also, I’m aware the code currently assumes that the 0 & 1 array values are there).

Is there a way to remove this redundancy? Somehow pass the result of x.Trim().Split(' ') to an anonymous function and then return an OrderByColumn object from there?

I know I could solve this issue using two for/foreach loops, but linq and lambdas are so cool! 🙂

  • 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-12T07:13:16+00:00Added an answer on June 12, 2026 at 7:13 am

    What about introducing a temporary variable inside the Select:

    sOrderBy.Split(',').Select(x => 
        {
            var trimmedSplitted = x.Trim().Split(' ');
            return new OrderByColumn()
            {
                ColumnName = trimmedSplitted[0].Trim(),
                IsAscending = (trimmedSplitted[1].Trim() == "ASC")
            };
        }
    ).ToList<OrderByColumn>()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string good overview of ESP's in more detail than you probably
I have a plist like this: <dict> <key>New item</key> <dict> <key>document</key> <string>driving licence</string> <key>Overview</key>
Overview I have an iOS app which sends local notifications at specific dates. I
Overview: I have an advancedDataGrid that I am using a GroupingCollection on and I
Overview I have an iOS project which contains 2 navigation controllers as shown in
I have an overview drawing of a plant and I want to show the
I have a overview page containing a list with some links from which multiple
I have a document that is an overview document within a folder, there are
So I have been tasked with modifying the Stories Overview report for TFS. I
Problem Overview: My application has an enrollment form. Users have a habit of entering

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.