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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:33:00+00:00 2026-05-30T09:33:00+00:00

I have a list of ID numbers that I pull from a session variable

  • 0

I have a list of ID numbers that I pull from a session variable and convert to an Array for use in a LINQ query that will then be used to populate a dropdown list. This is basically a list of recent clients that have been accessed by the program. I would like it to show them in the order they were last accessed.

Example array values (66, 78, 55, 24, 80)

After the LINQ query, it orders them by ID by default: (24, 55, 66, 78, 80)

So when it shows in the dropdownlist it is sorted this way when all I really needed was for it to stay in the order of the array values, only reversed: (80, 24, 55, 78, 66)

Code:

// Grab pre-built session variable with comma delimited int values
string RecentClients = Session["RecentClients"].ToString();

// Convert to Array
int[] RecentClientsArray = 
Array.ConvertAll(RecentClients.Split(','), n => Convert.ToInt32(n));

// Display in gridview to see current order of array (For debug purposes)
gridArray.DataSource = RecentClientsArray;
gridArray.DataBind();

// Setup LINQ
DataClasses1DataContext db = new DataClasses1DataContext();

// Populate RecenClientList using RecentClientsArray to compare to ConsumerID
var RecentClientList = 
(from c in db.ClientNames
 where RecentClientsArray.Contains(c.ConsumerID)
 select new { FullName = c.LastName + ", " + c.FirstName + " #" + c.ConsumerID, 
              recentConsumerID = c.ConsumerID});


// Display in gridview to see new order after populating var RecentClientList 
// (for debug purposes)
gridList.DataSource = RecentClientList;
gridList.DataBind();

// Set and bind values for dropdown list.
ddLastClients.DataSource = RecentClientList;
ddLastClients.DataTextField = "FullName";
ddLastClients.DataValueField = "recentConsumerID";
ddLastClients.DataBind();

EDIT Corrected Code using accepted solution:

// Grab pre-built session variable with comma delimited int values
string RecentClients = Session["RecentClients"].ToString();

// Convert to Array
int[] RecentClientsArray = Array.ConvertAll(RecentClients.Split(','), n => Convert.ToInt32(n));

// Setup LINQ
DataClasses1DataContext db = new DataClasses1DataContext();

// Populate RecenClientList using RecentClientsArray to compare to ConsumerID
var RecentClientList = (from c in db.ClientNames
                        where RecentClientsArray.Contains(c.ConsumerID)
                        select new { FullName = c.LastName + ", " + c.FirstName + " #" + c.ConsumerID, recentConsumerID = c.ConsumerID });

var clients = (from item in RecentClientsArray
                join client in RecentClientList.ToList()
                on item equals client.recentConsumerID
                select client).Reverse().Distinct();

// Set and bind values for dropdown list.
ddLastClients.DataSource = clients;
ddLastClients.DataTextField = "FullName";
ddLastClients.DataValueField = "recentConsumerID";
ddLastClients.DataBind();
  • 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-30T09:33:01+00:00Added an answer on May 30, 2026 at 9:33 am

    I would leave your queries as you have written then and then handle the ordering in memory.

    Given

    var RecentClientsArray = ...
    var RecentClientList = ...
    

    You can use Enumerable.Join to bring these together. Join has the property of preserving the order of the outer sequence in the join operation. Use the join to get the initial order based on the array, and then use Reverse to fulfill the final part of your requirement. So you might simply have

    var clients = 
        (from item in RecentClientsArray 
         join client in RecentClientList.ToList() // materialize result from db
         on item equals client.recentConsumerID
         select client).Reverse();
    

    You should see the clients in the reverse order of the array, and then you would use this as the datasource on your controls.

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

Sidebar

Related Questions

I have a picker view that has a list of numbers to pick from.
I have a list of numbers coming from a database that range from 0.001
I have a list of phone numbers that have been dialed (nums_dialed). I also
I have a file that contain list of numbers that looks like this: 10^-92
I have a list that has some chapter numbers in string. When I sort
I have a text document that contains a list of numbers and I want
I have a list of numbers ( integers ) (say, from 1 to 10).
So i have a mysql query that queries a table contacts each contact then
I have a list of numbers that I want to put in a single
I have a list of ~1200 queries (part numbers) that are specified somewhere inside

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.