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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:28:38+00:00 2026-05-21T19:28:38+00:00

I am building a list of Users. each user has a FullName. I’m comparing

  • 0

I am building a list of Users. each user has a FullName.
I’m comparing users on FullName.

i’m taking a DataTable with the users from the old DB and parsing them to a ‘User’ Object. and adding them in a List<Users>. which in the code is a List<Deelnemer>

It goes like this:

    List<Deelnemer> tempDeeln = new List<Deelnemer>();
    bool dupes = false;
    foreach (DataRow rij in deeln.Rows) {
            Deelnemer dln = new Deelnemer();
            dln.Dln_Creatiedatum = DateTime.Now;
            dln.Dln_Email = rij["Ler_Email"].ToString();
            dln.Dln_Inst_ID = inst.Inst_ID;
            dln.Dln_Naam = rij["Ler_Naam"].ToString();
            dln.Dln_Username = rij["LerLog_Username"].ToString();
            dln.Dln_Voornaam = rij["Ler_Voornaam"].ToString();
            dln.Dln_Update = (DateTime)rij["Ler_Update"];
            if (!dupes && tempDeeln.Count(q => q.FullName.ToLower() == dln.FullName.ToLower()) > 0)
                dupes = true;
            tempDeeln.Add(dln);
     }

then when the foreach is done, i look if the bool is true, check which ones are the doubles, and remove the oldest ones.

now, i think this part of the code is very bad:

     if (!dupes && tempDeeln.Count(q => q.FullName.ToLower() == dln.FullName.ToLower()) > 0)

it runs for every user added, and runs over all the already created users.

my question: how would I optimize this.

  • 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-21T19:28:39+00:00Added an answer on May 21, 2026 at 7:28 pm

    You can use a set such as a HashSet<T> to track unique names observed so far. A hash-set supports constant-time insertion and lookup, so a full linear-search will not be required for every new item unlike you exising solution.

    var uniqueNames = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase);
    ...
    
    foreach(...)
    {
       ...
    
       if(!dupes)
       {
           // Expression is true only if the set already contained the string.
           dupes = !uniqueNames.Add(dln.FullName); 
       }
    }
    

    If you want to “remove” dupes (i.e. produce one representative element for each name) after you have assembled the list (without using a hash-set), you can do:

    var distinctItems = tempDeeln.GroupBy(dln => dln.FullName, 
                                            StringComparer.CurrentCultureIgnoreCase)
                                 .Select(g => g.First());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQMobile 1.0, and building a list of radio buttons dynamically from json
I am building a web site that has a wish list. I want to
I am building a web app which users use to assign tasks to each
I'm iteratively building a table that includes user information for a number of users,
I am building an application that tracks karma that users give to each other.
I am building the application that will load list of news from the website.
I am building the application that will load list of news from the website.
I am building a recipe app where a user can view recipes, list ingredients,
Typical SAX Parser - I am building a list out of the endElement. I
In my views.py, I'm building a list of two-tuples, where the second item in

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.