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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:07:20+00:00 2026-06-04T19:07:20+00:00

I want to write an linq extension (or a custom dictionary, sorted list list

  • 0

I want to write an linq extension (or a custom dictionary, sorted list list or whatever solution is best) that will allow me to add a value to a collection with its key being the ‘next available’ one.

For example:

int CreatedKey = IncrementDictionary.AddNext(myCustomer);

If the currently existing keys are as follows:

1
2
8
4
3

Then it would add the myCustomer into the dictionary with a key of 5 and it would return that key.

What do you think?

  • 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-04T19:07:22+00:00Added an answer on June 4, 2026 at 7:07 pm

    You can use SortedList with Extension method for Adding to next automatically retrieved key.

    Assuming Data structure be any object, with numeric key,

    Following is ExtensionMethod for SortedList

    public static class SortedListExtensions
    {
        ///Add item to sortedList (numeric key) to next available key item, and return key
        public static int AddNext<T>(this SortedList<int, T> sortedList, T item)
        {
            int key = 1; // Make it 0 to start from Zero based index
            int count = sortedList.Count;
    
            int counter=0;
            do
            {
                if (count == 0) break;
                int nextKeyInList = sortedList.Keys[counter++];
    
                if (key != nextKeyInList) break;
    
                key = nextKeyInList +1;
    
                if (count == 1 || counter == count  ) break;
    
    
                if (key != sortedList.Keys[counter])
                    break;
    
            } while (true);
    
            sortedList.Add(key, item);
            return key;
        }
    
    }
    

    It can be used like following

      SortedList<int, string> x = new SortedList<int, string>();
    
            x.Add(4, "BCD");
            x.Add(6, "BCD");
    
            x.AddNext("a");
            x.AddNext("b");
            x.AddNext("c");
            x.AddNext("d");
            x.AddNext("e");
    
            foreach (var item in x)
                Console.WriteLine(item.Key + " " + item.Value);
    

    The output is

            1 a
            2 b
            3 c
            4 BCD
            5 d
            6 BCD
            7 e
    

    You can use Dictionary, or any other data structure. In that case Double loop will be required. In case of SortedList, one loop is saved while searching key. This loop is internally used by SortedList.Add function using BinarySearch Algorithm.

    Binary search is faster than looping all elements (for larger size data).

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

Sidebar

Related Questions

this is my code that I write it but I want to use LINQ
I'm trying to write a LINQ expression that will join two tables and return
I want to write a new application that will use a DBMS. In the
I want to write an extension method for a collection of objects that uses
I want to be able to write extension methods so that I can say:
I want to write a LINQ to Entity query which does order by ascending
I want to write a syntax highlighting extension for Emacs, but I Googling of
I want to write a unit test for a Django manage.py command that does
I want to write a JAR file that makes use of the javax servlet
I have an enum(below) that I want to be able to use a LINQ

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.