Make a list containing distinct list items from dataset containing duplicates

I'm really stuck, and being as many of you guys that post solutions on here are by and large brilliant (IMHO), I figured I'd see what the best of you could make of this problem.

Some background

I'm trying to create a list that must contain only distinct items in a specific sequence. (it's a primary key and thus must be distinct (I didn't make it a primary key, but I have to work with what I'm given, you know how it goes).

For ease of understanding this requirement, think of creating a distinct list of recipe steps from a book of recipes. My problem is that the "cooks" of these "recipes" often change the order in which they create their masterpieces.

For instance:

Recipe 1
  • Whisk eggs using fork
  • Melt margarine in a skillet
  • Pour in the eggs
  • stir constantly
  • Plate
  • Add salt and pepper as desired
Recipe 2
  • Break eggs into bowl
  • Whisk eggs using fork
  • Melt margarine in a skillet over low heat
  • Pour in the eggs
  • stir constantly
  • Plate
  • Serve
  • Add salt and pepper as desired
Recipe 3
  • Whisk eggs using fork
  • Add salt and pepper as desired
  • Melt margarine in a skillet over low heat
  • Pour in the eggs
  • stir constantly
  • Plate

As you can tell "Add salt and pepper..." can't be number 2 in Recipe 3 and still be in the correct sequence in Recipes 1 and 2.

I think if I can ID the "offending" list item and add a period to the end of it, thus making it unique, this would work as a solution.

How do I do this in C# given a dataset (gotten by a SQL query) with duplicates in the correct sequence and placed into a List of type string? LINQ is not a requirement here, but I'm not afraid to use it if it provides a solution.

Specifically code (or psedo-code) that:

  • IDs the list item that needs to be duplicated and modified.
  • Determine WHERE in the newly created large list (assuming) is the newly modified list item to be placed.

If Your 1st question is going to be "show me your work", please be advised that I've done quite a bit of work on this, and the code is generally long. I am happy to work from either pseudo-code or try your code with my dataset. I'm also happy reading other solutions that may be pertinent.

Thanks, I look forward to seeing your solutions.

--edit: I'm starting to get the impression people don't like it if you don't post code. So here it goes (I said above it was long). The code works but it doesn't solve the problem. It returns a distinct list in order with no duplicates. (If the formatting below is bad please forgive)

    public void GetNewRecipeItemsFromDB(string RequestedRecipeName)
    {
        string connString = string.Empty;
        string strGetRecipeElements_Sql = "SQL that returns the dataset";
        string connString = GetConnectionString();

        using (SqlConnection conn = new SqlConnection(connString))
        {
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strGetRecipeElements_Sql;
            SqlDataReader reader = null;

            try
            {
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(strGetRecipeElements_Sql, conn);
                DataSet RecipeItems = new DataSet();
                adapter.Fill(RecipeItems, "RecipeItems");
                reader = cmd.ExecuteReader();
                List<string> RecipeItemList = new List<string>();

                //Create an array with existing RecipeItems

                int readerCount = 0;


                while (reader.Read())
                {
                    RecipeItems GSI = new RecipeItems();
                    GSI.RecipeItem = reader[0].ToString();
                    GSI.Sequence = Convert.ToInt32(reader[1].ToString());
                    GSI.Rank = Convert.ToInt32(reader[2].ToString());


                    RecipeItemList.Add(GSI.RecipeItem.ToString());
                    readerCount++;

                }
                string[] CurrentRecipeItemArray = new string[readerCount];
                string[] UpdatedRecipeItemArray = new string[readerCount];

                //RecipeItemList.Sort();
                label1.Text = "";
                textBox1.Text = "";
                CurrentRecipeItemArray = RecipeItemList.ToArray();

                for (int y = CurrentRecipeItemArray.Length - 1; y >= 0; y--)
                {


                    textBoxDBOrginal.Text += CurrentRecipeItemArray[y].ToString() + Environment.NewLine;
                }

                string[] lines = textBoxDBOrginal.Text.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                List<string> UniqueRecipeItemsInvertedList = new List<string>();
                if (lines.Length > 0)
                {
                    //OK it's not null lets look at it.
                    int lineCount = lines.Length;
                    string NewCompare = string.Empty;

                    for (int z = 0; z < lineCount; z++)

                    {
                        NewCompare = lines[z];
                        if (!UniqueRecipeItemsInvertedList.Contains(NewCompare))
                        {
                            UniqueRecipeItemsInvertedList.Add(NewCompare);

                        }
                    }
                }
                UniqueRecipeItemsInvertedList.Reverse();

                foreach (string s in UniqueRecipeItemsInvertedList)
                {
                    if (!string.IsNullOrEmpty(s))
                    {

                        listBox7.Items.Add(s.ToString());

                    }
                }


            }


            catch (SqlException ex)
            {
                MessageBox.Show(ex.Errors.ToString());

            }
            conn.Close();


        }



    }

Answers


The answer was already on this site. How to rename duplicates in list using LINQ

Code is:

      IEnumerable<String> GetUnique(IEnumerable<String> list)
    {
        HashSet<String> itms = new HashSet<String>();
        foreach(string itm in list)
        {
            string itr = itm;
            while(itms.Contains(itr))
            {
                itr = itr + "_";
            }
            itms.Add(itr);
            yield return itr;
        }
    }   

Need Your Help

nginx 504 Gateway Time-out

ruby-on-rails nginx passenger http-status-code-504

I'm running a rails3.0.7 project with phusion-passenger on nginx.

Brew install git putting files in .rvm

git rvm homebrew

After running brew install git, I got this output:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.